如何绑定到xaml中DataGridCheckBoxColumn的IsReadOnly属性?

时间:2016-11-09 16:34:45

标签: c# wpf xaml data-binding

我正在尝试绑定到IsReadOnly属性,但它似乎不起作用。我怎么能实现这个目标?我的做法有什么问题?下面是复制问题的示例代码。

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

4 个答案:

答案 0 :(得分:3)

这是因为<DataGridCheckBoxColumn>中有两种类型的元素样式,你需要为ElementStyle指定IsHitTestVisible="False"参数。

 <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>

答案 1 :(得分:0)

您可能需要在Mode上设置Binding

IsReadOnly={Binding IsReadOnly, Mode=OneWay}

默认Mode不能保证为OneWay - 它取决于基础DependencyProperty,因此最好只指定它。

答案 2 :(得分:0)

我无法理解为什么我在问题中的方法不起作用。但是,我找到了解决我问题的替代方案。我没有使用DataGridCheckBoxColumn,而是在datatemplate中使用DataGridTemplateColumnCheckbox。绑定工作正常。

            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding IsReadOnly}"
                                  IsEnabled="{Binding IsReadOnly}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

答案 3 :(得分:-1)

尝试将此添加到您的绑定中:

Binding="{Binding IsReadOnly, Mode=OneWay, UpdataSourceTrigger=PropertyChanged}" 

因此,UI会正确通知此类属性的任何更改,并正确更新控件的IsReadOnly属性。

相关问题