gpt4 book ai didi

c# mvvm绑定(bind)按钮命令参数来控制compositeCollection

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

我只是不明白:我有一个功能区菜单,其中一些选项卡/组/按钮在 xaml 中定义,一些选项卡/组/按钮在运行时定义(用户加载某个“命令集”)。为了实现这一点,我将在 xaml 中定义的ribbonTab 和一个collection 容器放在compositeCollection 中。

主窗口.xaml

<RibbonWindow x:Class="test_ribbonButtonBinding.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:local="clr-namespace:test_ribbonButtonBinding"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" DataContext=" {StaticResource svm}">
<RibbonWindow.Resources>
<CollectionViewSource x:Key="tabs" Source="{Binding ribbon}"/>
</RibbonWindow.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Ribbon x:Name="RibbonMenu" Grid.Column="0" Grid.Row="0">
<Ribbon.ItemsSource>
<CompositeCollection>
<RibbonTab Header="Testtab" >
<RibbonGroup x:Name="Testgroup" Header="Testgroup">
<RibbonButton LargeImageSource="Icons/question.png" Label="TestAddCommand" Command="{Binding cmdTestAddCommand}" />
<RibbonButton LargeImageSource="Icons/question.png" Label="TestReadCommand" Command="{Binding cmdTestReadCommand}" CommandParameter="{Binding SelectedItem, ElementName=lstSequence}"/>
</RibbonGroup>
</RibbonTab>
<CollectionContainer Collection="{Binding Source={StaticResource tabs}}"/>
</CompositeCollection>
</Ribbon.ItemsSource>
</Ribbon>
<ListView x:Name="lstSequence" ItemsSource="{Binding sequence}" Grid.Row="1" Grid.Column="0" SelectionMode="Extended"/>
</Grid>
</RibbonWindow>

现在已经定义按钮的 CommandParameter 出现问题:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=lstSequence'. BindingExpression:Path=SelectedItem; DataItem=null; target element is 'RibbonButton' (Name=''); target property is 'CommandParameter' (type 'Object')

这可能很简单,但我无法理解:如何将命令参数绑定(bind)到 ListView 中的 selectedItems。这显然是错误的dataContext。

更让我感到困惑的是:如果我省略了复合集合(所以所有选项卡都只是在 xaml 中定义),那么命令会正确绑定(bind)到 View 模型中的元素,并且命令参数会正确绑定(bind)到窗口中的元素(无论如何这如何工作,两者都应该绑定(bind)到 View 模型,因为它是在窗口的 DataContext 中指定的???)

任何帮助表示赞赏。

瑞苏

编辑:

最后在listview中设置一个setter属性解决了问题
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
</Style>
</ListBox.ItemContainerStyle>

但我仍然想知道为什么我的方法对其他任何解释都不起作用?

最佳答案

两个选项(未经测试):

1) 使用父元素通过 RelativeSource 传递值:

<Grid Tag={Binding SelectedItem, ElementName=lstSequence}>
...
<!-- hopefully there are no other grids on the way -->
<RibbonButton CommandParameter="{Binding Tag, RelativeSource={RelativewSource FindAncestor, AncestorType=Grid}}"
Command="{Binding cmdTestReadCommand}" .../>
...
<ListView x:Name="lstSequence" ... />
</Grid>

2)根本不使用参数
<ListView SelectedItem={Binding SelectedItem, Mode=OneWayToSource} ... />

查看模型:
public whatevertype SelectedItem { get; set; }

内部命令执行委托(delegate)
var parameter = SelectedItem;

关于c# mvvm绑定(bind)按钮命令参数来控制compositeCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32628125/

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