设置WpfToolkit datagrid列编辑样式

时间:2009-12-10 15:28:21

标签: wpf mvvm datagrid styles

如果特定条件为真,如何使单个datagrid列可编辑?

我在我的应用程序中使用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 Books绑定到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>

是否可以根据布尔值仅对单个datagrid列进行编辑?

1 个答案:

答案 0 :(得分: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>

不能很好地工作,但现在会做。欢迎任何其他建议。