简体   繁体   English

UserControl和嵌套DataGrid的依赖项属性

[英]Dependency Property on UserControl and nested DataGrid

I have a usercontrol like this: 我有这样的用户控件:

在此处输入图片说明

<UserControl>
    <ListView>
        <ListView.ItemTemplate>
            <DataTemplate>
                <DataGrid ItemSource="{Binding Contacts}">
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding Name}"/>
                        <DataGridTextColumn Binding="{Binding Address}" 
                                            Visibility="{Binding Path=ShowAddress, 
                                                Converter={StaticResource BoolToVisible}}"/>
                    </DataGrid.Columns>
                </DataGrid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</UserControl>

ShowAddress declaration: ShowAddress声明:

public bool ShowAddress
{
    get { return (bool)GetValue(ShowAddressProperty); }
    set { SetValue(ShowAddressProperty, value); }
}
public static readonly DependencyProperty ShowAddressProperty =
    DependencyProperty.Register("ShowAddress", typeof(bool), typeof(Contacts), new PropertyMetadata(true));

DataContext is null, i set the ListView ItemsSource as an IEnumerable of Persons DataContext为null,我将ListView ItemsSource设置为IEnumerable of Persons

public class Persons
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public List<Persons> Contacts { get; set; }
}

I need a dependency property of the UserControl hide or show certain columns of the datagrid in the listview. 我需要UserControl的依赖项属性在列表视图中隐藏或显示datagrid的某些列。 This way i can control which columns show, directly from the window that includes the usercontrol. 这样,我可以直接从包含usercontrol的窗口中控制显示哪些列。 Is this possible? 这可能吗?

I trying with the following code, I get an error on the output console: 我尝试使用以下代码,但在输出控制台上出现错误:

{Binding Path=ShowAddress, Converter={StaticResource BoolToVisible}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. System.Windows.Data错误:4:找不到参考'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.UserControl',AncestorLevel ='1''的绑定源。 BindingExpression:Path=ShowAddress; BindingExpression:路径= ShowAddress; DataItem=null; 的DataItem = NULL; target element is 'DataGridTextColumn' (HashCode=55248170); 目标元素是“ DataGridTextColumn”(HashCode = 55248170); target property is 'Visibility' (type 'Visibility') 目标属性为“可见性”(类型为“可见性”)

Maybe it can be done with a resource usercontrol, but the datagrid has no scope to the resource defined in the usercontrol. 也许可以使用资源用户控件来完成,但是数据网格对用户控件中定义的资源没有作用域。

Note: BoolToVisible is a converter to convert "true" into "Visible" and "false" into "Collapsed". 注意:BoolToVisible是一个转换器,可以将“ true”转换为“ Visible”,将“ false”转换为“ Collapsed”。

RelativeSource extension won't work for DataGridTextColumn (or any other DataGridColumn) because this column is not a part of the logical tree. RelativeSource扩展不适用于DataGridTextColumn(或任何其他DataGridColumn),因为此列不是逻辑树的一部分。 You could perform some workaround actions, for example: 您可以执行一些解决方法,例如:

<UserControl x:Class="Test31990722.Contacts"
             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" 
             xmlns:local="clr-namespace:Test31990722"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             Name="uc">

    <UserControl.Resources>
        <local:BoolToVisible x:Key="BoolToVisible" />
    </UserControl.Resources>
    <ListView Name="lv">
        <ListView.ItemTemplate>
            <DataTemplate>
                <DataGrid ItemsSource="{Binding Contacts}" AutoGenerateColumns="False"
                          Tag="{Binding ShowAddress, ElementName=uc,Converter={StaticResource BoolToVisible}}">
                    <DataGrid.Resources>
                        <TextBlock x:Key="FileNameColHeader" Text="Address" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}, Path=Tag}"/>
                    </DataGrid.Resources>
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding Name}"/>
                        <DataGridTextColumn Binding="{Binding Address}" Header="{StaticResource FileNameColHeader}"
                                            Visibility="{Binding Tag,  Source={StaticResource FileNameColHeader}}"/>
                    </DataGrid.Columns>
                </DataGrid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</UserControl>

Hope, it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM