如何将 WPF ListBoxItem 的 IsEnabled 属性 绑定到其源项的 属性?

How to bind the IsEnabled Property of a WPF ListBoxItem to a Property of its Source Item?

我有一个带有 ObservableCollection 作为 ItemSource:

的列表框
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 在文件转换完成后异步更新。
我认为我需要绑定此 属性 并将其放入 setter 以供 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 切换为启用?

我还可以在我的 DaemonFile 中添加一个 isEnabled 属性 来稍微简化一下。但这并没有解决我的绑定问题。

您不需要那个 ControlTemplate。

一个简单的 DataTrigger 应该可以工作:

<Style x:Key="ItemStyle" TargetType="{x:Type ListBoxItem}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding ProcessState}" Value="0">
            <Setter Property="IsEnabled" Value="False"/>
        </DataTrigger>
    </Style.Trigers>
</Style>

以防万一:

<ListBox x:Name="ListOfFiles"
         ItemContainerStyle="{StaticResource ItemStyle}" ...>