简体   繁体   中英

IValueConverter not being called for Visibility of a DataGridTextColumn

I have a DataGrid that will display columns based on a CheckBox . I know that I will need to create an IValueConverter for a bool to a Visibility . Problem is that my BoolToVisualConvertor is not even being called. Cannot figure out why. This is what I did:

public class BoolToVisualConvertor : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {
            if (value == null) return System.Windows.Visibility.Collapsed;
            bool data = (bool)value;
            if (data) return System.Windows.Visibility.Visible;
            return System.Windows.Visibility.Collapsed;
        }
        catch (Exception x)
        {
            return System.Windows.Visibility.Collapsed;
        }
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return false;
    }
}

Then in the XAML:

<Page.Resources>
    <local:BoolToVisualConvertor x:Key="BoolToVisaul" />
</Page.Resources>

CheckBox :

<CheckBox Name="MyCheckBox" IsChecked="True" />

DataGridTextColumn :

<DataGridTextColumn Header="MyData" Binding="{Binding Path=MyData}"  Visibility="{Binding IsChecked, ElementName=MyCheckBox, Converter={StaticResource BoolToVisaul}}" >

It has to be something simple that I am missing. Thanks!

edit: There was a mistake in the copy paste where I have a DataGrid that will display columns based on a CheckBox . I know that I will need to create an IValueConverter for a bool to a Visibility . Problem is that my BoolToVisualConvertor is not even being called. Cannot figure out why. This is what I did:

public class BoolToVisualConvertor : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {
            if (value == null) return System.Windows.Visibility.Collapsed;
            bool data = (bool)value;
            if (data) return System.Windows.Visibility.Visible;
            return System.Windows.Visibility.Collapsed;
        }
        catch (Exception x)
        {
            return System.Windows.Visibility.Collapsed;
        }
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return false;
    }
}

Then in the XAML:

<Page.Resources>
    <local:BoolToVisualConvertor x:Key="BoolToVisaul" />
</Page.Resources>

CheckBox :

<CheckBox Name="MyCheckBox" IsChecked="True" />

DataGridTextColumn :

<DataGridTextColumn Header="MyData" Binding="{Binding Path=MyData}"  Visibility="{Binding IsChecked, ElementName=MyCheckBox, Converter={StaticResource BoolToVisaul}}" />

It has to be something simple that I am missing. Thanks!

edit: there was a mistake in the copy and paste, now reads correctly

edit 2: Doing more testing it appears the issue is in the DataGrid. Created a test TextBlock outside the DataGrid like this;

<TextBlock Text="{Binding Path=MyValue}" Visibility="{Binding IsChecked, ElementName=MyCheckBox, Converter={StaticResource BoolToVisaul}}" />

That works perfectly. The thing that really confuses me now is that if I do this:

<DataGridTextColumn Header="MyData" Binding="{Binding Path=MyData}"  Visibility="Collapsed" />

That works...

You have to do it with styles. Try this:

<DataGridTextColumn Header="MyData" Binding="{Binding MyData}">
    <DataGridTextColumn.HeaderStyle>
        <Style TargetType="DataGridColumnHeader" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
            <Style.Setters>
                <Setter Property="Visibility" Value="{Binding IsChecked, ElementName=MyCheckBox, Converter={StaticResource BoolToVisualConvertor}}" />
            </Style.Setters>
        </Style>
    </DataGridTextColumn.HeaderStyle>
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
            <Style.Setters>
                <Setter Property="Visibility" Value="{Binding IsChecked, ElementName=MyCheckBox, Converter={StaticResource BoolToVisualConvertor}}" />
            </Style.Setters>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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