2009-12-10 21 views
1

如何使一個單一的數據網格列可編輯,如果一個特定的條件爲真?設置WpfToolkit的數據網格列編輯風格

我在我的應用程序中使用MVVM模式。

Model :: 

public class Book : INotifyPropertyChanged 
{ 
    public int BookId {get; set;} 

    public string Title {get; set;} 

    public string SerialNumber {get; set;} 

    public bool CanEditSerialNumber {get; set;} // Allows editing serialnumber if this property is set to true. 

} 

視圖模型::

public class MyViewModel : INotifyPropertyChanged 
{ 
    DbEntities _dbEntities; // ADO.Net entity model. 

    public ObservableCollection<Book> Books {get; set;} 

    public MyViewModel() 
    { 
     this.ListAllBooks(); 
    } 

    public void ListAllBooks() 
    { 
     _dbEntities = new DbEntities(); 

     var book = from _book in _dbEntities.Book 
        select new Book() 
        { 
         BookId = _book.BookID, 
         Title = _book.Title 
         SerialNumber = _book.ISBN, 
         CanEditSerialNumber = _book.HasSerialNumber 
        } 

      Books = new ObservableCollection<Book>(book); 
      OnPropertyChanged("Books"); 
    } 

} 

查看:: 我綁定的ObservableCollection書籍到WpfToolkit數據網格。

<WpfToolkit:DataGrid Name="dgBooks" 
        ItemSource = {Binding Books} 
        ....> 

    <WpfToolkit.DataGrid.Columns> 

     <!-- Here I want to display Book Title and SerialNumber --> 

     <CustomControls:LabelTextBoxColumn Binding={Binding Title} 
              ElementStyle={StaticResource myLabelStyle} 
              /> 

     <!-- This column should be editable only if CanEditSerialNumber property is set to true. --> 
     <CustomControls:LabelTextBoxColumn Binding={Binding SerialNumber} 
              ElementStyle={StaticResource myLabelStyle} 
              EditElementStyle={StaticResource myTextBoxStyle}/> 

    </WpfToolkit.DataGrid.Columns> 

是否有可能使基於布爾值的單個數據網格列可編輯?

回答

0

現在,這是我做了什麼:

<CustomControls:LabelTextBoxColumn.EditElementStyle> 

     <Style TargetType="{x:Type TextBox}"> 

      <Style.Triggers> 
       <Trigger Property={Binding CanEditSerialNumber} Value="False"> 
       <Setter Property="IsReadOnly" Value="True"> 
       </Trigger> 
      </Style.Triggers> 

     </Style> 

    </CustomControls:LabelTextBoxColumn.EditElementStyle> 

不能很好地工作,但現在會做。歡迎任何其他建議。