在 WPF 的 UserControl 中显示/隐藏 DataTemplate 中的控件

发布于 2024-12-04 20:30:20 字数 1233 浏览 1 评论 0原文

我有一个 UserControl,有一个带有 DataTemplate 的 ListView 控件,在 DataTemplate 中我定义了一个要根据 UserControl 的名为 ShowCheckBox 的属性显示的复选框。

如何获取对 UserControl 的引用,以便我可以执行以下操作:

<UserControl x:Class="WpfApplication15.UserControl2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <ListView>
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" Width="500">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Visibility="{Binding ??? this.ShowCheckBox ??? }" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</UserControl>

I have a UserControl, there is a ListView control with DataTemplate, in the DataTemplate I define a CheckBox to be shown based on a property of the UserControl called ShowCheckBox.

How do I get the reference to the UserControl so I can do some thing like:

<UserControl x:Class="WpfApplication15.UserControl2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <ListView>
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" Width="500">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Visibility="{Binding ??? this.ShowCheckBox ??? }" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</UserControl>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2024-12-11 20:30:20

您可以使用 BooleanToVisibilityConverter相对源绑定

<UserControl x:Class="WpfApplication15.UserControl2"
             ...>
    <UserControl.Resources>
       <BooleanToVisibilityConverter x:Key="Converter" />
    </UserControl.Resources>
    <ListView>
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" Width="500">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Visibility="{Binding 
                                      RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, 
                                      Path=ShowCheckBox, 
                                      Converter={StaticResource Converter}}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</UserControl>

You could to use a BooleanToVisibilityConverter and a RelativeSourceBinding.

<UserControl x:Class="WpfApplication15.UserControl2"
             ...>
    <UserControl.Resources>
       <BooleanToVisibilityConverter x:Key="Converter" />
    </UserControl.Resources>
    <ListView>
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" Width="500">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Visibility="{Binding 
                                      RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, 
                                      Path=ShowCheckBox, 
                                      Converter={StaticResource Converter}}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</UserControl>
奶茶白久 2024-12-11 20:30:20

调用您的用户控件,如下:x:Name="This"
并且比:

    <Visibility={Binding ElementName=This, Path=MyProperty}>

假设您的属性是 Visibility 类型(如果它是 bool 您应该按照另一个答案中的建议使用 BoolToVisibilityConverter )

Call your UserControl, this: x:Name="This"
And than:

    <Visibility={Binding ElementName=This, Path=MyProperty}>

Assuming your property is of type Visibility (if it's bool you should use BoolToVisibilityConverter as suggested in another answer)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文