我在DataGrid的DataTemplate中遇到可见性绑定问题。
我对可见性的绑定是
Visibility="{Binding ElementName=Hauptfenster, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}"
我添加了一个TextBox,显示了IsAdmin的值:
<TextBlock Text="Visible or Not" Width="150" Height="30" Visibility="{Binding ElementName=Hauptfenster, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}" HorizontalAlignment="Right" />
和一个可见或不可用的TextBlock
<TextBlock Text="Visible or Not" Width="150" Height="30" Visibility="{Binding ElementName=Hauptfenster, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}" HorizontalAlignment="Right" />
在另一台机器上,两个TextBlock元素正确地更改它们的值(true / false或Visible / Collapsed)。只有数据网格中的复选框不会更改它:( 为什么呢?
整个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>
转换器:
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();
}
}
如果“IsAdmin”属性为false,则应折叠复选框(或其他)。它在我的机器上工作正常(.NET 4.5.1),但是如果我在安装了.NET 4.0的另一台机器上试用它,它就不起作用了。该项目的目标框架是.NET 4.0。
我做错了什么?这是一个.NET错误吗?有任何想法吗?谢谢!
答案 0 :(得分:1)
谢谢,这是解决方案!
<CheckBox Content="Test" Visibility="{Binding Source={x:Reference Hauptfenster}, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}" />
现在它就像一个魅力!