gpt4 book ai didi

c# - 如何在 xaml 中绑定(bind)到 DataGridCheckBoxColumn 的 IsReadOnly 属性?

转载 作者:行者123 更新时间:2023-11-30 16:00:14 31 4
gpt4 key购买 nike

我正在尝试绑定(bind)到 IsReadOnly 属性,但它似乎不起作用。我怎么能做到这一点?我的方法有什么问题?下面是复制问题的示例代码。

更新:添加了隐藏文件的代码...我有一个可观察的集合作为隐藏代码的属性挂起,用作数据上下文。问题不在于属性更改时,即使我第一次绑定(bind)它时,选中的属性已正确绑定(bind),但 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; }
}
}

最佳答案

因为<DataGridCheckBoxColumn>里面有两种元素样式, 你需要指定 IsHitTestVisible="False" ElementStyle 的参数。

 <DataGridCheckBoxColumn x:Name="col1" 
Binding="{Binding IsReadOnly}"
IsReadOnly="{Binding IsReadOnly}"
ElementStyle="{StaticResource ReadOnlyCheckBoxStyle}"
Header="With Binding"
Width="SizeToHeader"/>

还有像这样的 ReadOnlyCheckBoxStyle

<Style x:Key="ReadOnlyCheckBoxStyle" TargetType="{x:Type CheckBox}">
<Setter Property="IsHitTestVisible" Value="False"/>
</Style>

关于c# - 如何在 xaml 中绑定(bind)到 DataGridCheckBoxColumn 的 IsReadOnly 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40511823/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com