1
votes

I have a usercontrol like this:

enter image description here

<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:

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

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. This way i can control which columns show, directly from the window that includes the 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''. BindingExpression:Path=ShowAddress; DataItem=null; target element is '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".

1
Please share your ShowAddress declaration and DataContext setting if existsVMaleev
Done. I edit the question.Pythonizo
<DataTemplate ItemSource="{Binding Contacts}"> what's this?VMaleev
Is the binding to the "contacts" foreach persons listed in the listviewPythonizo
DataTemplate has no ItemSource or ItemsSource properties. Perhaps, ItemsSource is related to ListView?VMaleev

1 Answers

1
votes

RelativeSource extension won't work for DataGridTextColumn (or any other DataGridColumn) because this column is not a part of the logical tree. 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