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!
Pregunta
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!
Solución
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!