WPF通过MVVM中的另一个组合在一个组合中更改选定的值



我有问题。我首先在WPF中有两个组合:

<ComboBox commands:PropertyChangeBehavior.Command="{Binding GetCurrentsModuleCommand}" SelectedIndex="0">
  <ComboBox.ItemsSource>
    <CompositeCollection>
      <ComboBoxItem IsEnabled="True">All</ComboBoxItem>
      <CollectionContainer Collection="{Binding Source={StaticResource ResourceKey=AxmModulesCombo}}" />
    </CompositeCollection>
   </ComboBox.ItemsSource>
 </ComboBox>
</StackPanel>

让我们称其为源,它可以束缚命令:

public ICommand GetCurrentsModuleCommand => new Command(module =>
{
   SelectedAxmModule = module.ToString();
   Stuff(module);
});

此组合框没有SelectItem属性(嗯,它不需要任何东西,因为参数仅传递给方法(。

和目标combxbox

<ComboBox ItemsSource="{Binding AxmModules}" 
    SelectedItem="{Binding SelectedAxmModule, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
    SelectedValuePath="{Binding SelectedAxmModules}"
    IsSynchronizedWithCurrentItem="True"/>

巫婆选择的属性是:

private string selectedAxmModule;
public string SelectedAxmModule
{
    get => selectedAxmModule;
    set
    {
       selectedAxmModule = value;
       OnPropertyChanged();
    }
 }

现在,这些组合框的值相似,现在我想在源命令中看到源上的值,我想从源中选择相同的值这是无关紧要的(。

我尝试添加更新属性和SelectedValuePath,但是在选择源中的值之后,目标组合框中没有良好的价值是空的,并且OnPropertyChange可以按详细信息起作用。有没有办法实现这一目标?

如果AxmModulesIEnumerable<byte>,则SelectedAxmModule应该是byte属性。您不能将string属性设置为byte值。类型必须匹配。

SelectedValuePath使用SelectedItem时是多余的。

因为您有

IsSynchronizedWithCurrentItem="True"/>

则选择的元素应为电流。您应该使用CollectionView的当前项目,而不是绑定SelectedItem。您可以阅读更多,可能比您想阅读的更多有关CollectionView和此类内容的阅读:https://social.technet.microsoft.com/wiki/wiki/contents/articles/26673.wpf-collectionview-tips.aspx

也是如此。看到这个

SelectedValuePath="{Binding SelectedAxmModules}"

选定的valuepath是字符串而不是绑定。它应该是您要使用的任何字段的名称。如果出于某种原因,您要绑定到集合中的当前项目,则可以使用/符号。https://social.technet.microsoft.com/wiki/wiki/contents/articles/29859.wpf-tips-bind-bind-to-current-current-current-em-collection.aspx

我不确定这将如何与复合收藏集合。我更喜欢单独的控件来指示"所有"的选择,因此不会以这种方式使用复合收藏。您可能需要考虑自己。

最新更新