我試圖安裝並綁定ToggleButton
以更改DataGrid
中列的可見性。 I am following this post問題在於轉換器不會啓動,我不知道爲什麼。WPF DataGridTemplateColumn集具有綁定的可見性未觸發
我的代碼如下:
<DataGridTemplateColumn Header="My Header"
Visibility="{Binding IsChecked,
ElementName=AdvancedToggleButton,
Converter={StaticResource booleanToVisaulConverter}}" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<!-- Elements removed for brevity-->
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
的轉換器是本
<Page.Resources>
<local:BooleanToVisaulConverter x:Key="booleanToVisaulConverter" />
</Page.Resources>
隨着後面的代碼;
public class BooleanToVisaulConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool visibility = (bool)value;
return visibility ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
Visibility visibility = (Visibility)value;
return (visibility == Visibility.Visible);
}
}
編輯:@Breeze發現booleanToVisaulConverter
實際上並沒有指向BooleanToVisaulConverter
一個錯誤。這已被修復,但仍未解僱。詢問ToggleButton代碼;
<ToggleButton Name="AdvancedToggleButton" Content="Advanced" />
如果錯誤不在於你用BooleanToVisibiltyConverter在靜態的資源,而不是BooleanToVisaulConverter – Breeze
@Breeze固定BooleanToVisaulConverter,見上面 – Xaphann
有已經在框架BooleanToVisibilityConverter,僅供參考,請分享advancedToggleButton的聲明。你應該使用像Snooper這樣的工具來檢查可視化樹在運行時的綁定錯誤。 – Will