简体   繁体   English

按钮 state(启用/禁用)取决于 SelectedItem 属性

[英]Button state (enabled/disabled) depending on SelectedItem property

When user select a row in Listview bound to an ObservableCollection of elements, the state of a button (outside the listview) should change depending of SelectedItem Property.当用户 select 绑定到 ObservableCollection 元素的 Listview 中的一行时,按钮(在列表视图之外)的 state 应该根据 SelectedItem 属性而改变。

It works on Selection change with a converter but if user keep the row selected and if the item property changed it does not change the button state它适用于使用转换器进行选择更改,但如果用户保持选中行并且如果项目属性发生更改,则不会更改按钮 state

Here is the button and list xaml这是按钮和列表 xaml

<Button x:Name="btPause"  
        IsEnabled="{Binding Path=SelectedItem, ElementName=LstFiles,  Converter={StaticResource StateToBoolConverter }}"
        Width="121" Margin="0,10,0,10"
        Content="Pause"/>
<ListView  SelectionMode="Single" x:Name="LstFiles"  ItemsSource="{Binding LocalFiles}" Height="273.4" UseLayoutRounding="False" Margin="10">
    <ListView.View>
        <GridView  >
            <GridViewColumn Header="DisplayName" DisplayMemberBinding="{Binding DisplayName}"  Width="200"/>
            <GridViewColumn Header="FileState" DisplayMemberBinding="{Binding FileState}"  Width="100"/>
        </GridView>
    </ListView.View>
</ListView>

I used a converter:我用了一个转换器:

if (!(value is FileInfo l_fileInfo)) return false;
bool ret = (l_fileInfo.FileState == FileState.Uploading ? true : false);
return ret;

The VM:虚拟机:

class FilesInfoViewModel:INotifyPropertyChanged
{
    private ObservableCollection<FileInfo> _localFiles;
    public ObservableCollection<FileInfo> LocalFiles
    {
        get { return _localFiles; }
        set { _localFiles = value; OnPropertyChanged("LocalFiles"); }
    }

    public FilesInfoViewModel()
    {
        _localFiles = new ObservableCollection<FileInfo>
        {
            new FileInfo { DisplayName = "Test", FileState = FileState.Uploading }
        };
    }


    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Model Classes Model 类

public class FileInfo : INotifyPropertyChanged
{

    private FileState _fileState;
    public FileState FileState
    {
        get { return _fileState; }
        set { _fileState = value; OnPropertyChanged("FileState"); }
    }

    private string _displayName;
    public string DisplayName
    {
        get { return _displayName; }
        set { _displayName = value; OnPropertyChanged("DisplayName"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

public enum FileState
{
    Uploading,
    Downloading,
    Connecting,
    Error,
    Waiting,
    Finishing,
    Paused
}

Hope it is clear enough希望足够清楚

bind directly to state:直接绑定到 state:

IsEnabled="{Binding Path=SelectedItem.FileState, ElementName=LstFiles,  Converter={StaticResource StateToBoolConverter}}"

converter should be modified accordingly:转换器应相应修改:

return (value is FileState fileState) && (fileState == FileState.Uploading);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM