gpt4 book ai didi

c# - 为 ListBoxItem 绑定(bind) mouseLeftButtonDown 并在 PropertyChanged 上绑定(bind)背景颜色(mvvm、wpf)

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:39 25 4
gpt4 key购买 nike

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

这个问题是两个问题,但它是关于相同的代码,所以我想我可以在同一个线程中提出这个问题。

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

为此,我使用了一个 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 时,我需要将命令绑定(bind)到“MouseLeftButtonDown”事件。我尝试了 ACB 解决方案,但无法正常工作。最后我尝试了上面的代码,当我点击一个项目时我无法触发它,但是在项目下方,在列表框的空白部分(而不是在列表框项目上),它会按预期触发。我想我需要重新排列我的 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 - 命令被触发(但没有任何东西被“选中”)

问题二

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

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

希望你能帮助我,因为我现在没有头发可以拔了;-)

最佳答案

如果您只想获取所选项目,则无需使用事件处理程序。我已经为您构建了一个工作示例,以展示如何仅使用绑定(bind)(MVVM)来实现您在问题中的要求:

C#( View 模型):

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>

结果:您可以看到带有CanDeliver = True (Amount>0) 的Flow 项具有绿色背景。 TextBlock 正在显示 SelectedFlow

enter image description here

关于c# - 为 ListBoxItem 绑定(bind) mouseLeftButtonDown 并在 PropertyChanged 上绑定(bind)背景颜色(mvvm、wpf),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37116846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com