将 ComboBoxItem 绑定到 WPF 中的命令?

klu*_*991 0 wpf

我想要一个<ComboBox>ItemsSource 绑定到某个ViewModel集合,但我希望第一个<ComboBoxItem>绑定到ICommand属性,并将其命名(例如)“将新元素添加到集合... ”。有什么建议?

Sza*_*zsi 5

你可以这样做:

在您的视图模型,你有CommandItems收集你想设置为ItemsSource

public class MainWindowViewModel : INotifyPropertyChanged
{
    public MainWindowViewModel()
    {
        Items = new ObservableCollection<string>()
        {
            "John Doe",
            "Lara Croft",
            "Sam Fisher"
        };

        AddNewItemCommand = new DelegateCommand(OnAddNewItem);
    }

    private void OnAddNewItem()
    {
        Items.Add("New Item");
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private ObservableCollection<string> _items;
    public ObservableCollection<string> Items
    {
        get { return _items; }
        set
        {
            _items = value;
            NotifyPropertyChanged(nameof(Items));
        }
    }

    public ICommand AddNewItemCommand { get; set; }

    [NotifyPropertyChangedInvocator]
    protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
Run Code Online (Sandbox Code Playgroud)

CompositeCollection在 XAML 中使用 a在绑定集合前添加“添加新项目”:

<ComboBox>
    <ComboBox.Resources>
        <CollectionViewSource x:Key="ItemsCollection" Source="{Binding Path=Items}" />
    </ComboBox.Resources>
    <ComboBox.ItemsSource>
        <CompositeCollection>
            <ComboBoxItem FontStyle="Italic">
                <i:Interaction.Behaviors>
                    <local:UnselectableComboBoxItemBehavior />
                </i:Interaction.Behaviors>
                <TextBlock>
                <Hyperlink Command="{Binding AddNewItemCommand}">Add new item</Hyperlink>
                </TextBlock>
            </ComboBoxItem>
            <CollectionContainer Collection="{Binding Source={StaticResource ItemsCollection}}" />
        </CompositeCollection>
    </ComboBox.ItemsSource>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)

并创建一个Behavior以抑制选择第一个项目,即“添加新项目”项目:

public class UnselectableComboBoxItemBehavior : Behavior<ComboBoxItem>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.PreviewMouseDown += AssociatedObjectOnPreviewMouseDownAndMouseLeftButtonUp;
        AssociatedObject.PreviewMouseLeftButtonUp += AssociatedObjectOnPreviewMouseDownAndMouseLeftButtonUp;
    }

    private void AssociatedObjectOnPreviewMouseDownAndMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (e.Source == sender)
        {
            e.Handled = true;
        }
    }

    protected override void OnDetaching()
    {
        AssociatedObject.PreviewMouseDown -= AssociatedObjectOnPreviewMouseDownAndMouseLeftButtonUp;
        AssociatedObject.PreviewMouseLeftButtonUp -= AssociatedObjectOnPreviewMouseDownAndMouseLeftButtonUp;
        base.OnDetaching();
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

在此处输入图片说明