0
votes

I have a problem with a visibility binding in a DataTemplate of a DataGrid.

My binding for visibility is

Visibility="{Binding ElementName=Hauptfenster, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}"

I've added a TextBox which displays the value of IsAdmin:

<TextBlock Text="Visible or Not" Width="150" Height="30" Visibility="{Binding ElementName=Hauptfenster, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}" HorizontalAlignment="Right" />

and a TextBlock which is visible or not

<TextBlock Text="Visible or Not" Width="150" Height="30" Visibility="{Binding ElementName=Hauptfenster, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}" HorizontalAlignment="Right" />

On the other machine, the two TextBlock-elements are changing their values correctly (true/false or Visible/Collapsed). Only the Checkbox in the datagrid won't change it :( Why?

Whole XAML:

<Window x:Class="BindingTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    x:Name="Hauptfenster">
<Window.Resources>
    <BooleanToVisibilityConverter x:Key="TestConvert" />
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="20*"/>
        <RowDefinition Height="87*"/>
    </Grid.RowDefinitions>

    <ToggleButton Content="TestButton" Width="150" Height="30" Click="ToggleButton_Click" />

    <TextBlock Text="Visible or Not" Width="150" Height="30" Visibility="{Binding ElementName=Hauptfenster, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}" HorizontalAlignment="Right" />

    <TextBlock Text="{Binding ElementName=Hauptfenster, Path=Test.IsAdmin}" Width="100" Height="20" HorizontalAlignment="Left"  />

    <DataGrid x:Name="dgTest" Grid.Row="1">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Spalte 1">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox Content="Test" Visibility="{Binding ElementName=Hauptfenster, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}" />
                            <TextBlock Text="{Binding Spalte1}" />
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Spalte 2">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox Content="Test" />
                            <TextBlock Text="{Binding Spalte2}" />
                        </StackPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Header="Spalte 3" Binding="{Binding Spalte3}" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

The Converter:

public class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ((bool)value == true) ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

The checkbox (or whatever) should be collapsed, if the "IsAdmin" property is false. It works fine on my machine (.NET 4.5.1), but if i try it on another machine with .NET 4.0 installed, it don't work. The target framework for this project is .NET 4.0.

What i'm doing wrong? Is this a .NET bug? Any Ideas? Thanks!

1
Run debug in your TestConvert. Verify what is coming in. - paparazzo
I would guess that IsAdmin is false on the other machine. Verify the value of IsAdmin in the converter and then trace back to see why it's false when you think it should be true. - ChrisF♦
Maybe you deployed a different config on the other machine? - Jan Rothkegel
I think that <DataGrid.Columns> got major improvements in .net 4.5 that is why ElementName Binding is working. In .net 4.0 DataGrid.Columns often lost LogicalTree and therefore couldn't create Binding properly. However to make sure it works in both .net worlds you could set the source of binding to x:reference Hauptfenster :) - dev hedgehog
Thanks, this is the solution! <CheckBox Content="Test" Visibility="{Binding Source={x:Reference Hauptfenster}, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}" /> Now it works like a charm! - Sheik

1 Answers

1
votes

Thanks, this is the solution!

<CheckBox Content="Test" Visibility="{Binding Source={x:Reference Hauptfenster}, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}" />

Now it works like a charm!