Setting WpfToolkit datagrid column editing style
How to make a single datagrid column editable if a particular condition is true?
I'm using MVVM pattern in my application.
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.
}
ViewModel::
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");
}
}
View:: I bind the ObservableCollection Books to a WpfToolkit datagrid.
<WpfToolkit:DataGrid Name="dgBooks"
ItemSource = {Binding Books}
....>
<WpfToolkit.DataGrid.Columns>
<!-- Here I want to display Book Title and SerialNumber -->
<CustomCont开发者_运维百科rols: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>
Is it possible to make only a single datagrid column editable based on a boolean value?
For now, this is what I have done:
<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>
Does not work perfectly, but will do for now. Any other suggestions are welcome.
精彩评论