gpt4 book ai didi

wpf - 设置 WpfToolkit 数据网格列编辑样式

转载 作者:行者123 更新时间:2023-12-03 10:51:21 25 4
gpt4 key购买 nike

如果特定条件为真,如何使单个数据网格列可编辑?

我在我的应用程序中使用 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.

}

View 模型::
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 绑定(bind)到 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>

是否可以根据 bool 值仅使单个数据网格列可编辑?

最佳答案

现在,这就是我所做的:

   <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>

不能完美地工作,但现在可以。欢迎任何其他建议。

关于wpf - 设置 WpfToolkit 数据网格列编辑样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1881749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com