我試圖綁定到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