2016-11-09 38 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> 
相關問題