【问题标题】:MVVM Binding one ComboBox from anotherMVVM 将一个 ComboBox 与另一个绑定
【发布时间】:2016-04-10 14:00:42
【问题描述】:

我对 MVVM 很陌生,我想问一下,您是否可以帮助我将 SelectedValue 从 ComboBox 获取到我的 VM-Class。在我的示例中,我有一个包含所有汽车品牌的 ComboBox。如果我选择一个汽车品牌,我希望在另一个 ComboBox 中拥有该汽车品牌的所有型号以供选择。我已经进行了一些研究,但我找不到解决我问题的单一解决方案。

After clicking on the Brand-ComboBox.

到目前为止这很好,但是当我想要显示模型时,ComboBox 保持为空。

来自我的View-Class的代码:`

<ObjectDataProvider x:Key="vmcars" ObjectType="{x:Type local:VMCars}"/>

<!--MainGrid-->
<Grid DataContext="{StaticResource vmcars}">
.
.
.
.
</Grid>

来自我的品牌组合框的代码:

        <!--CarBrand Combobox-->
        <ComboBox x:Name="carBrand" Style="{StaticResource combobox}" 
                  ItemsSource="{Binding AllCars}"
                  Grid.Column="1" Grid.Row="1"
                  DisplayMemberPath="c_brand"
                  Margin="20,13,17,15" 
                  VerticalAlignment="Center" Height="30">
            <ComboBox.SelectedItem>
                <Binding Path="SelectedBrand"                  
                         BindsDirectlyToSource="True"                  
                         Mode="OneWayToSource" />
            </ComboBox.SelectedItem>
        </ComboBox>

我的Model-ComboBox中的代码:

        <!--CarModel ComboBox-->
        <ComboBox x:Name="carModel" Style="{StaticResource combobox}" Grid.Column="1"
                  Margin="20,15,17,14"
                  ItemsSource="{Binding ModelSelectedBrand}" DisplayMemberPath="c_model"
                  Grid.Row="2" VerticalAlignment="Center" Height="30">
        </ComboBox>

我的 VMClass 中的代码:

    public string selectedBrand;
    public string SelectedBrand
    {
        get { return selectedBrand; }
        set
        {
            selectedBrand = value;
            PropertyChanged(this, new PropertyChangedEventArgs
                               ("ModelSelectedBrand"));

        }
    }

        public IEnumerable<c_car> ModelSelectedBrand             
    {
        get
        {
            return (from c in db.c_car
                    where c.c_brand.Equals(SelectedBrand) //THIS IS CAUSING PROBLEMS

                    select c).GroupBy(x=>x.c_model).Select(x=>x.FirstOrDefault()).OrderBy(x=>x.c_model).ToList();  
        }
    }

当我在 Linq 语句中注释 WHERE 子句时,我得到以下结果: click here

这些是所有车型。

我认为问题在于我的“SelectedBrand”属性没有从 carBrand-ComboBox

中获得任何价值

因此,我想问您,是否有人知道可能导致麻烦的原因。

【问题讨论】:

  • 为什么要评论 WHERE 子句?您的 where 语句中是否有编译时错误,或者您有其他意外结果?
  • 在评论 WHERE 子句之前,它没有在我的 ComboBox 中显示任何模型。评论只是一个示例,向您展示 SelectedBrand 属性没有从 Brand-ComboBox 接收任何 SelectedItem .
  • 您的 c_brand 值或 SelectedBrand 值似乎为空,或者值不同。您可以向该行添加一个新断点以显示 SelectedBrand 值(您的代码看起来不错,它应该包含实际选择的值和汽车集合及其 c_brand 值以具体化问题。

标签: c# wpf xaml mvvm combobox


【解决方案1】:

您的汽车品牌 ComboBox 当前正在将类名称 c.car 传递给您的查询。如果您绑定到SelectedValue 而不是SelectedItem,并将SelectedValuePath 设置为"c_car",它会起作用。

所以你的新 CarBrand ComboBox 定义将是

<!--CarBrand Combobox-->
<ComboBox x:Name="carBrand" Style="{StaticResource combobox}" 
          ItemsSource="{Binding AllCars}"
          Grid.Column="1" Grid.Row="1"
          DisplayMemberPath="c_brand"
          SelectedValuePath="c_brand"
          Margin="20,13,17,15" 
          VerticalAlignment="Center" Height="30">
    <ComboBox.SelectedValue>
        <Binding Path="SelectedBrand"                  
                 BindsDirectlyToSource="True"                  
                 Mode="OneWayToSource" />
    </ComboBox.SelectedValue>
</ComboBox>

【讨论】:

  • 感谢您的回答。在我删除 `BindsDirectlyToSource="True" Mode="OneWayToSource"` 后它起作用了。
  • 很高兴听到。没问题。
猜你喜欢
  • 1970-01-01
  • 2021-11-02
  • 2017-05-14
  • 1970-01-01
  • 1970-01-01
  • 2012-06-22
  • 1970-01-01
  • 2011-10-28
  • 1970-01-01
相关资源
最近更新 更多