2
votes

Suppose I have two checkBoxes namely chkBox1 and chkBox2.

Now I have a textBlock named txtBlock, whose visibility depends on IsChecked Property of chkBox1 as well as chkBox2. i.e. If chkBox1.IsChecked = true and chkBox2.IsChecked = true Then only txtBlock should be Visible.

Now I hava a DataGrid with say 2 Columns. namely colID and colName. Both the Columns are DataGridTemplateColumns. I want to make colName Visible only if txtBlock.Visibility = Visible.

Here is XAML :

<Window........>

    <CheckBox x:Name="chkBox1" />

    <CheckBox x:Name="chkBox2" />

    <TextBlock x:Name="txtBlock">
        <MultiBinding Converter="{StaticResource twiceBoolToVisibilityConverter}">
            <Binding ElementName="chkBox1" Path="IsChecked" />
            <Binding ElementName="chkBox2" Path="IsChecked" />
        </MultiBinding>
    </TextBlock>

    <DataGrid>
        <DataGridTemplateColumn Header="ID">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Name" Visibility="{Binding Path=Visibility, ElementName=txtBlock}">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid>

</Window>

But this binding in DataGridTemplateColumn will not work as DataGrid Column is not a part of Visual Tree. So, I created a Class Called BindingProxy as follows:

public class BindingProxy : Freezable
{
    #region Overrides of Freezable

    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    #endregion

    public object Data
    {
        get { return GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }

    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}

And then declared a StaticResource as Below :

<DataGrid.Resources>
    <helpers:BindingProxy x:Key="proxy" Data="{Binding Visibility, ElementName=txtBlock}" />
</DataGrid.Resources>

And then I tried to bind DataGridTemplateColumn as follows :

<DataGridTemplateColumn Header="Name" Visibility="{Binding Source={StaticResource proxy}}">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

But still the binding is not successful.

Can anybody ssuggest a good solution?

1
Thanks for the proxy trickWpfNewbie
@WpfNewbie You are welcome :)Vishal

1 Answers

2
votes

Data dependency property of BindingProxy holds textBlock Visibility, so you need to bind with it.

Change

Visibility="{Binding Source={StaticResource proxy}}"

to

Visibility="{Binding Data, Source={StaticResource proxy}}"

and your column visibility will work fine.