【发布时间】:2021-09-25 03:41:31
【问题描述】:
我有一个 ObservableCollection 为 ItemSource 的 ListBox:
public partial class HomePage : Page
{
public ObservableCollection<DaemonFile> Files { get; private set; }
private readonly MainWindow MainWindow;
public HomePage(MainWindow MainWindow)
{
Files = new ObservableCollection<DaemonFile>();
this.MainWindow = MainWindow;
InitializeComponent();
ListOfFiles.ItemsSource = Files;
}
...
}
我的DaemonFile 中有以下属性:
public class DaemonFile : INotifyPropertyChanged
{
public enum ProcessStates : int
{
CREATED = 0,
CONVERTED = 1,
UPLOADING = 2,
FINISHED = 3
}
private ProcessStates ProcessStateValue = ProcessStates.CREATED;
public ProcessStates ProcessState
{
get { return this.ProcessStateValue; }
set
{
if (value != this.ProcessStateValue)
{
this.ProcessStateValue = value;
NotifyPropertyChanged();
}
}
}
...
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
mainWindow.WriteLine("Property " + propertyName + " Changed!");
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
} else
{
mainWindow.WriteLine("Cant fire event!");
}
}
}
文件转换完成后,ProcessState 会异步更新。
我想我需要绑定此属性并将其放入IsEnabled 的设置器中。
<Style x:Key="Item" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}">
</ContentPresenter>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding ProcessState}" Value="0">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
每当DaemonFile 将其ProcessState 更改为1 或更高时,我如何才能实现相应的ListBoxItem 切换为启用?
- 我不希望用户能够在文件完成转换之前上传文件
我还可以将isEnabled 属性添加到我的DaemonFile 以简化一些事情。但这并不能解决我的绑定问题。
【问题讨论】:
标签: c# wpf data-binding wpf-controls listboxitem