0
我在DataGrid的DataTemplate中的可見性綁定存在問題。可見性綁定不能在不同的PC上工作
我的可見性結合是
Visibility="{Binding ElementName=Hauptfenster, Path=Test.IsAdmin, Converter={StaticResource TestConvert}}"
我已經添加了顯示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或Visi BLE /摺疊)。只有在數據網格的複選框不會改變它:( 爲什麼
全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錯誤?有任何想法嗎?謝謝!
在TestConvert中運行調試。驗證什麼是進來 – Paparazzi
我想'IsAdmin'在另一臺機器上是錯誤的。驗證轉換器中「IsAdmin」的值,然後追溯查看爲什麼它錯誤,當你認爲它應該是真實的。 – ChrisF
也許你在另一臺機器上部署了不同的配置? –