在PropertyChanged(mvvm,wpf)上为ListBoxItem和背景颜色绑定mouseLeftButtonDown

时间:2016-05-09 13:19:26

标签: c# wpf xaml mvvm listbox

抱歉,如果您认为这是另一篇文章的重复,但我尝试了所有可能的解决方案,但我无法使其发挥作用。

问题是两个部分,但它是关于相同的代码,所以我想我可以在同一个帖子中提出问题。

我尝试在没有任何(硬编码)耦合的情况下使用mvvm在C#,wpf,vs2015中进行订购系统。我需要做两件事。首先是触发我需要在ViewModel中捕获的事件,其次,当文章数量超过一定水平时,该文章的listboxitem背景应为绿色(否则为白色/灰色)

为此我使用ListBox和一些listBoxItems。

MainWindow.xaml(重要的部分)

        <Window x:Class="Sequence_Application_2.GUI.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:acb="clr-namespace:AttachedCommandBehavior;assembly=AttachedCommandBehavior"
        mc:Ignorable="d"
        ....
    <Window.DataContext>

<Grid Grid.Column="0" Margin="10">
                <ListBox x:Name="LstViewFlows" SelectedItem="{Binding SelectedFlow.Name}" ItemsSource="{Binding Flows.Keys}" >
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="{x:Type ListBoxItem}" >
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding SelectedFlow.CanDeliver, UpdateSourceTrigger=PropertyChanged}" Value="true" >
                                    <Setter Property="ListBoxItem.Background" Value="DarkGreen" />
                                    <Setter Property="FontWeight" Value="Bold"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ListBox.ItemContainerStyle>
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseLeftButtonDown">
                            <i:InvokeCommandAction Command="{Binding SetSelectedCommand}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </ListBox>
            </Grid>

问题1)

当我单击列表框中的listboxItem时,我需要将命令绑定到“MouseLeftButtonDown”事件。我试过ACB解决方案,但无法让它工作。最后我尝试了上面的代码,当我点击一个项目时,我无法触发它,但是在项目下方,在列表框的空白部分(不是在listboxitem上),它应该触发它。猜猜我需要重新安排我的xaml文件或其他东西,但我试了两天,但现在似乎没什么用。

该命令名为“setSelectedCommand”,并按此实现

internal class SetSelectedFlowCommand : ICommand
{
    private FlowsViewModel _flowsViewModel;

    public SetSelectedFlowCommand(FlowsViewModel flowsViewModel)
    {
        _flowsViewModel = flowsViewModel; 
    }

    /// <summary>
    /// 
    /// </summary>
    public event EventHandler CanExecuteChanged
    {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
        _flowsViewModel.SetSelectedFlow();
    }
}

正如我所说,如果我点击ListBoxItem - 没有任何事情发生,但如果我点击ListBox - 命令被触发(但没有“选择”)

问题2

正如您在上面的xaml中看到的,我尝试根据存储在ViewModel中的字典中的对象中的值.CanDeliver来设置ListBoxitem的背景颜色。变量Flow是字典,SelectedFlow应该是选择的流。

这是一个订单系统,CanDeliver是一个变量,告诉操作员/用户是否有足够的产品交付给客户。如果有足够的listboxitem应为绿色,否则保持白色/灰色。你明白我的问题吗?我可以这样做吗?引用字典中的对象? (它由对象中的INotifyPropertyChanged触发)

希望你能帮助我,因为我现在没有更多的头发可以从头脑中拉出来了; - )

1 个答案:

答案 0 :(得分:0)

如果您只想获取selectedItem,则无需使用事件处理程序。我已经为您构建了一个工作示例,以展示如何仅使用绑定(MVVM)来满足您的问题:

C#(ViewModel):

using System;
using System.Collections.Generic;
using System.Windows;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MyViewModel mvm = new MyViewModel()
            {
                Flows = new ObservableCollection<Flow>() 
                { 
                    new Flow() { Name = "Flow1" }, 
                    new Flow() { Name = "Flow2" }, 
                    new Flow() { Name = "Flow3" , Amount=1}, 
                    new Flow() { Name = "Flow4" } 
                }
            };
            this.DataContext = mvm;
        }
    }

    public class MyViewModel : ObservableObject
    {
        private Flow _selectedflow;
        public ObservableCollection<Flow> Flows
        {
            get;
            set;
        }

        public Flow SelectedFlow
        {
            get { return _selectedflow; }
            set
            {
                if (value != _selectedflow)
                {
                    _selectedflow = value;
                    RaisePropertyChanged("SelectedFlow");
                }
            }
        }
    }

    public class Flow : ObservableObject
    {
        private string _name;
        private int _amount;
        public string Name
        {
            get { return _name; }
            set
            {
                if (value != _name)
                {
                    _name = value;
                    RaisePropertyChanged("Name");
                }
            }
        }

        public bool CanDeliver
        {
            get
            {
                return Amount > 0;
            }
        }

        public int Amount
        {
            get { return _amount; }
            set
            {
                if (value != _amount)
                {
                    _amount = value;
                    RaisePropertyChanged("Amount");
                    RaisePropertyChanged("CanDeliver");
                }
            }
        }
    }

    public class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected void RaisePropertyChanged(String propertyName)
        {
            OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
    }
}

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="350">
    <Grid>
        <StackPanel Orientation="Vertical">
        <ListBox SelectedItem="{Binding SelectedFlow}" ItemsSource="{Binding Flows}" DisplayMemberPath="Name">
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}" >
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding CanDeliver, UpdateSourceTrigger=PropertyChanged}" Value="true" >
                            <Setter Property="Background" Value="DarkGreen" />
                            <Setter Property="FontWeight" Value="Bold"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
        <TextBlock Text="{Binding SelectedFlow.Name}"/>
        </StackPanel>
    </Grid>
</Window>

结果:您可以看到FlowCanDeliver = TrueAmount>0)具有绿色背景。 TextBlock显示SelectedFlow

enter image description here