【问题标题】:Calling of a method from another binding in a combobox从组合框中的另一个绑定调用方法
【发布时间】:2011-11-21 11:21:43
【问题描述】:

我有一个组合框,我想从 MainViewModel 调用一个方法,但它绑定到EmployeesOverviewViewModel。是否有可能做到这一点?如果是 - 如何?

这是我的组合框代码

<ComboBox ScrollViewer.CanContentScroll="False" Text="Select Employees" DataContext="{Binding EmployeesOverviewViewModel, Source={StaticResource ViewModelLocator}}" Name="employeeComboBox" ItemsSource="{Binding Employees}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding Path=IsSelected}" Content="{Binding Path=Name}" Width="{Binding ElementName=employeeComboBox, Path=ActualWidth}" VerticalAlignment="Center">
            </CheckBox>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

我曾考虑过使用 Command,但无法解决绑定问题。

BR

【问题讨论】:

  • 包含组合框的窗口/面板/页面是否将其数据上下文设置为 MainViewModel 的实例?如果是这样,您可以使用查找父级并绑定到其数据上下文的绑定。
  • 带有组合框的窗口有 mainviewmodel 作为 datacontext,但组合框有 employeesoverviewmdel som datacontext
  • 我添加了一个答案,其中包含如何绑定到父数据上下文的示例。

标签: c# wpf .net-4.0 combobox command


【解决方案1】:

如果您的 MainViewModel 是作为 DataContext 的可视化树的任何位置,那么您可以在您的 ComboBox 的 CommandBinding 中使用 RelativeSource 来实现您想要的。

【讨论】:

    【解决方案2】:

    扩展我对原始帖子的评论。下面显示的是如何绑定到父级的数据上下文的示例。

    Command="{Binding RelativeSource={RelativeSource FindAncestor,
        AncestorType={x:Type Window}}, Path=DataContext.SomeCommand}"
    

    在要绑定的视图模型上设置命令的路径。

    【讨论】:

      【解决方案3】:

      两个 ViewModel 是否相互认识?然后EmployeesOverviewViewModel 可以提供一个您将执行的委托,MainWindowViewModel 可以使用此委托将其“绑定”到它的方法。 (编辑:如果MainWindowViewModel 知道EmployeesOverviewViewModel 就足够了)

      否则,您可以尝试使用FindAncestor 的绑定并尝试获取MainWindowView。那么问题就在于您不仅需要视图本身,还需要它的DataContext

      【讨论】:

        【解决方案4】:

        我会说你有两种现实的方法。

        1) 如果您的视图可以看到合适的 MainViewModel 实例,您应该能够使用 StaticResource 或 DynamicResource 绑定到命令或您需要的任何内容。我看到您正在使用 StaticResource 来查找提供 ComboBox 项目的视图模型,对于查找 MainViewModel 是否有类似的工作?

        2) 如果您的视图看不到 MainViewModel,但您的 viewmodel 可以,请让 viewmodel 公开合适的命令或方法,然后调用 MainViewModel 的版本。这样更简洁,因为您的 EmployeesOverviewView 根本不需要了解有关 MainViewModel 的任何信息。

        我更喜欢选项 2。

        【讨论】:

          【解决方案5】:

          您可以使用 MultiValueConverter 来做到这一点。我给你看一个小样本。

          我有一个带有 3 个 CheckBox 的窗口:

          <Window x:Class="WpfApplication7.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              Title="MainWindow" 
              Height="300" Width="400" mc:Ignorable="d" 
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
              xmlns:WpfApplication7="clr-namespace:WpfApplication7" d:DesignHeight="371" 
              d:DesignWidth="578" SizeToContent="WidthAndHeight">
              <Window.Resources>
                  <WpfApplication7:MultiBooleanConverter x:Key="multiBooleanConverter" />
              </Window.Resources>    <Grid>
                  <CheckBox Content="Hallo">
                      <CheckBox.IsChecked>
                          <MultiBinding Converter="{StaticResource ResourceKey=multiBooleanConverter}">
                              <Binding ElementName="checkBox1" Path="IsChecked"/>
                              <Binding ElementName="checkBox2" Path="IsChecked"/>
                          </MultiBinding>
                      </CheckBox.IsChecked>
                 </CheckBox>
                  <CheckBox x:Name="checkBox1" Content="CheckBox" Height="16" HorizontalAlignment="Left" Margin="64,72,0,0" VerticalAlignment="Top" />
                  <CheckBox Content="CheckBox" Height="16" HorizontalAlignment="Left" Margin="62,120,0,0" x:Name="checkBox2" VerticalAlignment="Top" />
              </Grid>
          </Window>
          

          MultiValueConverter 是这样声明的:

          public class MultiBooleanConverter : IMultiValueConverter
          {
              public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
              {
                  return values.Cast<bool>().Any(b => b);
              }
          
              public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
              {
                  object[] returnValue = new object[targetTypes.Length];
                  for (int i = 0; i < targetTypes.Length; i++)
                  {
                      returnValue[i] = (bool)value;
                  }
                  return returnValue;
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2016-08-23
            • 2012-07-24
            • 2015-10-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-09-16
            相关资源
            最近更新 更多