【发布时间】:2011-07-16 22:26:00
【问题描述】:
我正在尝试将布尔值绑定到 GridViewColumn 中的复选框,但它不起作用。我什至尝试只返回 false,但复选框看起来仍然启用。只有当我在 xaml 中输入“False”时它才有效。
绑定的属性是:
public bool HasPermissions
{
get { return this.UserPrivileges == UserPrivileges.FullAccess; }
}
this.UserPrivileges 的当前值不是UserPrivileges.FullAccess。
Xaml 代码:
<Window x:Class="EffectsWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Effects Manager"
Width="800"
Height="500"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
<DockPanel VerticalAlignment="Stretch">
<DockPanel.Resources>
<ListView x:Name="EffectsListView"
ItemsSource="{Binding AllEffects}">
<ListView.View>
<GridView>
<GridViewColumn Width="50" Header="Override">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Margin="0"
HorizontalAlignment="Center"
IsEnabled="{Binding HasPermission}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</DockPanel>
</Window>
编辑:当前属性代码:
public bool HasPermissions
{
get { return this.UserPermissions == UserPermissions.FullAccess; }
set { this.RaisePropertyChanged ( "HasPermissions" ); }
}
【问题讨论】:
-
你确定你已经实现了 INotifyPropertyChanged 吗?
-
谢谢,我没有实现它。我认为只有在 2 路绑定时才需要 INotifyPropertyChanged?就像值只定义一次一样,你还需要它吗?
-
对不起还有个意思,这个属性不能设置,只能get。
-
是的,只要您想将属性中的更改反映到 UI 上,您就需要这样做。试试吧。让我们知道它是否有效。
-
在这种情况下,您要做的是在 UserPriviliges 属性调用 OnNotifyPropertyChanged("HasPermissions") 的 Setter 中
标签: c# .net data-binding listview gridviewcolumn