簡體   English   中英

MVVM從另一個綁定一個ComboBox

[英]MVVM Binding one ComboBox from another

我對MVVM很陌生,我想問一下,是否可以幫助我將SelectedValue從ComboBox轉換為我的VM-Class。 在我的示例中,我有一個包含所有汽車品牌的ComboBox。 如果我選擇了一個汽車品牌,我想在另一個ComboBox中選擇該汽車品牌的所有型號。 我已經進行了一些研究,但無法找到解決問題的單一方法。

單擊品牌組合框后。

到目前為止,這很好,但是當我要顯示模型時,ComboBox保持空白。

我的View-Class中的代碼:

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

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

來自我的Brand-ComboBox的代碼:

        <!--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子句時,得到以下結果: 單擊此處

這是所有的車型。

我認為問題是我的“ SelectedBrand ”屬性沒有從carBrand-ComboBox中獲得任何價值

因此,我想問你,是否有人知道什么可能造成麻煩。

您的汽車品牌ComboBox當前正在將c.car的類名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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM