我试图绑定到IsReadOnly
属性,但它似乎不工作。我怎么能做到这一点?我的方法有什么问题?以下是复制问题的示例代码。如何绑定到xaml中的DataGridCheckBoxColumn的IsReadOnly属性?
更新: 添加的代码隐藏文件...我有一个观察集合挂从后面的代码,它被用作数据上下文的属性。问题不在属性更改时,即使我第一次绑定它时,检查的属性绑定正确,但IsReadonly不是。
public class ModelClass:INotifyPropertyChanged
{
private bool m_IsReadOnly;
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propName)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
public bool IsReadOnly
{
get { return m_IsReadOnly; }
set
{
m_IsReadOnly = value;
OnPropertyChanged("IsReadOnly");
}
}
}
<Window x:Class="TestWpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestWpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="63*"/>
<RowDefinition Height="17*"/>
</Grid.RowDefinitions>
<DataGrid x:Name="modelClassDataGrid"
AutoGenerateColumns="False"
EnableRowVirtualization="True"
ItemsSource="{Binding Models}"
Grid.RowSpan="2" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.Columns>
<DataGridCheckBoxColumn x:Name="col1"
Binding="{Binding IsReadOnly}"
IsReadOnly="{Binding IsReadOnly}" //doesn't work
Header="With Binding"
Width="SizeToHeader"/>
<DataGridCheckBoxColumn x:Name="col2"
Binding="{Binding IsReadOnly}"
IsReadOnly="True"
Header="Without Binding"
Width="SizeToHeader"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="324,163,0,0" VerticalAlignment="Top"/>
</Grid>
public partial class MainWindow : Window
{
private ObservableCollection<ModelClass> _models = new ObservableCollection<ModelClass>(new List<ModelClass>()
{
new ModelClass() {IsReadOnly = false},
new ModelClass() {IsReadOnly = true},
new ModelClass() {IsReadOnly = false},
});
public MainWindow()
{
InitializeComponent();
}
public ObservableCollection<ModelClass> Models
{
get { return _models; }
}
}
尝试'绑定= “{结合IsReadOnly,模式=双向}”'在第二DataGridCheckBoxColumn。 – Clemens
我有完全相同的问题,设置文字值的作品,但设置与绑定的值不起作用,即使绑定是正确的。这似乎是我的WPF错误 – rafael