2016-11-09 78 views
2

我试图绑定到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; } 
    } 
} 
+0

尝试'绑定= “{结合IsReadOnly,模式=双向}”'在第二DataGridCheckBoxColumn。 – Clemens

+0

我有完全相同的问题,设置文字值的作品,但设置与绑定的值不起作用,即使绑定是正确的。这似乎是我的WPF错误 – rafael

回答

0

您可能需要设置ModeBinding

IsReadOnly={Binding IsReadOnly, Mode=OneWay} 

默认Mode不能保证是OneWay - 这取决于底层DependencyProperty上所以最好还是指定。

+0

指定模式属性也无法正常工作。 – NoSaidTheCompiler

+0

在调试过程中,您是否收到VS中显示的任何绑定错误? – toadflakz

-1

尝试这也添加到您的绑定:

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

所以UI正确通知的任何改变这一类财产,并正确地更新控制的IsReadOnly属性。

+0

谢谢你,但指定模式也没有帮助。 – NoSaidTheCompiler

+0

如果在属性设置器中使用RaisePropertyChanged(“IsReadOnly”),它会更简单,因为它是引发OnPropertyChanged事件的方法。 –

+0

@FrançoisBoivin这里的方法叫做'OnPropertyChanged',它在属性设置器中被调用。事件本身被命名为“PropertyChanged”。除此之外,在单向绑定中设置'UpdataSourceTrigger'是毫无意义的。它只对TwoWay或OneWayToSource绑定产生影响。 – Clemens

0

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

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

这是因为有两种类型的<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> 
相关问题