gpt4 book ai didi

c# - WPF 命令如何获取所有必要的信息以添加到正确的列表项

转载 作者:行者123 更新时间:2023-12-03 10:35:48 32 4
gpt4 key购买 nike

我正在开发一个用户控件,如果我想遵守 MVVM 模型,我想知道如何正确地实现它。

我将我的真实用例缩短为一个示例:
有一个名为 config.xaml 的配置 View 和一个名为 ConfigVM 的 View 模型。对于这个例子,我认为模型是不相关的,但如果你需要它来回答你的问题,我会称之为 ConfigData。

在此 View 中,有一个带有字符串值的组合框。应将组合框选定值添加到列表框的选定项。要执行此操作,在组合框旁边有一个“添加名称”按钮。与命令绑定(bind)。
但是:列表框的选定项由主列表框的选定项定义。所以这有点级联。
我希望这张照片能为设置带来一些亮点。
User Control setup

mvvm 类的编码是这样的(缩写来理解这个想法):

class ConfigVM
{
public ObservableList list1{}
}

class List1ItemVm
{
public ObservableList List2{}
}

class List2Item
{
public string Name{}
}

当前按钮的绑定(bind)是(当然不工作):
      Command="{Binding AddCommand}"
CommandParameter="{Binding SelectedItem, ElementName=comboBox}"

我的问题是:如何让接收 list2 选定项添加组合框选定项?
从命令中我得到组合框选定项的名称作为参数,但是在命令内部的点我不知道我应该将它添加到哪个列表中。

Ann.:我添加了级联列表,因为这是我的场景,这让我更难获得正确的项目并在 xaml 中进行绑定(bind)。

我能想到的可能:
  • 我应该引用 ViewModel 中的 View 吗?找出list2的选定项目?
    这可能是对 MVVM 模式的突破?正确的?
    (在 DataContextChanged 上设置 DataContext 时,我可以将 View 设置为 VM 的父级。)
  • 或者我可以以某种方式绑定(bind)它,让我拥有所有可用的信息,以某种方式自动将名称添加到 list2item 中? (如何?)
  • 我应该尝试多重绑定(bind)吗?我以前没有使用过它,我觉得这个“简单”的问题有点过于复杂。 (写转换器等)

  • 更新
    来自真实代码的更多信息:
    组合框:
    <StackPanel Grid.Row="0"
    Orientation="Horizontal"
    Grid.Column="1">
    <ComboBox x:Name="comboSymbols"
    HorizontalAlignment="Left"
    MinWidth="150"
    Margin="10,10,0,0"
    ItemsSource="{Binding SymbolsAvailable}"
    dd:DragDrop.IsDragSource="True"
    Height="22"
    VerticalAlignment="Top">
    <ComboBox.ItemTemplate>
    <DataTemplate>
    <StackPanel Orientation="Horizontal">
    <TextBlock Text="{Binding Name}" />
    <TextBlock Text=" | " />
    <TextBlock Text="{Binding Unit}" />
    </StackPanel>
    </DataTemplate>
    </ComboBox.ItemTemplate>
    </ComboBox>
    <Button Content="Symbol hinzufügen"
    HorizontalAlignment="Left"
    MinWidth="75"
    Margin="10,10,0,0"
    Command="{Binding AddCommand}"
    CommandParameter="{Binding SelectedItem, ElementName=comboSymbols}"
    Height="22"
    VerticalAlignment="Top" />
    </StackPanel>

    list 2:
    <ListBox x:Name="symbolsListBox"
    Grid.Column="1"
    HorizontalAlignment="Left"
    Height="175"
    Margin="26,39,0,0"
    Grid.Row="1"
    VerticalAlignment="Top"
    Width="137"
    DataContext="{Binding Path=SelectedItem, ElementName=channelListBox}"
    ItemsSource="{Binding Symbols}">
    <ListBox.ItemTemplate>
    <DataTemplate>
    <StackPanel Orientation="Horizontal">
    <TextBlock Text="{Binding Name}" />
    <TextBlock Text=" | " />
    <TextBlock Text="{Binding Unit}" />
    </StackPanel>
    </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox>

    list 1:
            <ListBox x:Name="channelListBox" HorizontalAlignment="Left"
    Width="170"
    Margin="10,10,0,10"
    ItemsSource="{Binding ChannelCollection}"
    dd:DragDrop.IsDragSource="True"
    dd:DragDrop.IsDropTarget="True"
    dd:DragDrop.DropHandler="{Binding}"
    Height="504">
    <ListBox.ItemTemplate>
    <DataTemplate>
    <StackPanel Orientation="Horizontal">
    <TextBlock Text="{Binding Gid}" />
    <TextBlock Text=" | " />
    <TextBlock Text="{Binding Name}" />
    </StackPanel>
    </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox>

    最佳答案

    备注 :这个答案是在与 OP 的问题下进行的对话的汇总。

    Should I reference the view inside the ViewModel? To find out the selected Item of list2? This is probably a break to the MVVM Pattern? correct? (I could set the view as a parent of the VM when the DataContext is set on DataContextChanged.)



    在纯 MVVM 中, View 模型 (VM) 应该与 View (V) 分离,因此不应包含对底层 V 的类对象的引用。所以不行。

    Or can I bind it in a way where I have all the information available to automatically somehow magic add the Name to the list2item? (how?)



    是的。您可以将 V 数据绑定(bind)到 选定的东西添加时在您的虚拟机中,反之亦然。

    WPF 绑定(bind)可以是单向的;两种命名方式,但有两种。这意味着 listbox 中的选定项目可以绑定(bind)到虚拟机,虚拟机也可以设置选中项。当您认为 VM 负责填充 listbox其余的很容易。

    因此,您的 XAML 中的许多元素看起来都在相互引用,尤其是 SelectedItem和你的 添加命令 它使用了一个参数,因此遗憾的是 VM 在所有情况下都没有直接看到它。

    "You mean I can add a property SelectedItem to the VM and bind it"



    是的,这是正确的,是的,这仍然是 MVVM。 XAML 元素相互引用也没有错(这就是力量),但在这种情况下,我可能会有一个用于 SelectedItem 的 VM 属性。

    你想了解更多吗?
  • WPF Apps With The Model-View-ViewModel Design Pattern
  • 5: Implementing the MVVM Pattern Using the Prism Library 5.0 for WPF
  • 关于c# - WPF 命令如何获取所有必要的信息以添加到正确的列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30012554/

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