基于Silverlight DataGrid中另一个ComboBox的SelectedItem更新ComboBox ItemsSource

时间:2014-06-06 12:49:19

标签: silverlight dynamic datagrid combobox selecteditem

我在ComboBoxes内有2 DataGrid。我想在第一个ComboBox中进行选择时更新第二ItemsSourceComboBox。我刚开始学习Silverlight,我正在尝试遵循MVVM模式。那是迄今为止我所拥有的。

模型

public class Country : ViewModelBase
{
    private string name;
    public string Name {...}
    public string Code { get; set; }
}
public class City : ViewModelBase
{
    private string name;
    public string Name {...}        
    public string Code { get; set; }
}
public class Location : ViewModelBase
{
    private City city;
    public City City {...}

    private Country country;
    public Country Country {...}
}

视图模型

public class CountryCityViewModel : ViewModelBase
{
    private Location selectedLocation;
    public Location SelectedLocation {...}

    IRepo Repo { get; set; }
    public ObservableCollection<Location> Locations { get; set; }
    public ObservableCollection<Country> Countries { get; set; }
    public ObservableCollection<City> Cities { get; set; }       

    public CountryCityViewModel(IRepo repo)
    {
        this.Repo = repo;

        Countries = new ObservableCollection<Country>(repo.LoadCountries());
        Cities = new ObservableCollection<City>();
        Locations = new ObservableCollection<Location>(repo.LoadLocations());

        this.PropertyChanged += CountryCityViewModel_PropertyChanged;

    }

    private void UpdateCityComboBoxItemsSource(object obj)
    {
        Country selCountry = ( (Country) obj );
        if (obj != null)
        {
            UpdateCities(selCountry);
        }
    }

    void CountryCityViewModel_PropertyChanged(object sender, 
                       System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "SelectedLocation")
        {
            UpdateCities(SelectedLocation.Country);
        }
    }
    private void UpdateCities(Country country)
    {
        var newCities = Repo.LoadCities(country);
        Cities.Clear();
        foreach (var city in newCities)
        {
            Cities.Add(city);
        }
        OnPropertyChanged("Cities");
    }
}

XAML

<Custom:CustomDataGrid x:Name="CountryCity" AutoGenerateColumns="False" 
                       ColumnWidth="*" RowHeight="25"
                       ItemsSource="{Binding Path=Locations}" 
                       SelectedItem="{Binding Path=SelectedLocation, 
                       Mode=TwoWay}">
        <Custom:CustomDataGrid.Columns>
            <sdk:DataGridTemplateColumn Header="Country">
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Country.Name}"/>
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
                <sdk:DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox x:Name="CountryComboBox" 
                                  ItemsSource="{Binding 
                                  RelativeSource={RelativeSource
                                  AncestorType=Custom:CustomDataGrid}, 
                                  Path=DataContext.Countries}"
                                  SelectedItem="{Binding Path=Country, 
                                  Mode=TwoWay}"
                                  DisplayMemberPath="Name">
                           <i:Interaction.Triggers>
                             <i:EventTrigger EventName="SelectionChanged">
                               <i:InvokeCommandAction  Command="{Binding 
                                  RelativeSource={RelativeSource 
                                  AncestorType=Custom:CustomDataGrid},                                                                                               
                                  Path=
                                  DataContext.                                 
                                   CountryComboBoxItemSelectedCommand}"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </ComboBox>
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellEditingTemplate>
        </sdk:DataGridTemplateColumn>
            <sdk:DataGridTemplateColumn Header="City">
            <sdk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=City.Name}"/>
                </DataTemplate>
            </sdk:DataGridTemplateColumn.CellTemplate>
            <sdk:DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <ComboBox x:Name="CityyComboBox" 
                       ItemsSource="{Binding 
                       RelativeSource={RelativeSource 
                       AncestorType=Custom:CustomDataGrid}, 
                       Path=DataContext.Cities}"
                       SelectedItem="{Binding Path=City, Mode=TwoWay}" 
                       DisplayMemberPath="Name"/>
                 </DataTemplate>
            </sdk:DataGridTemplateColumn.CellEditingTemplate>
        </sdk:DataGridTemplateColumn>
    </Custom:CustomDataGrid.Columns>
</Custom:CustomDataGrid>

首先,我尝试捕捉PropertyChanged SelectedLocation事件。但这只会在我选择数据网格的下一行时更新Cities。这不是所需的功能。我想在Cities中进行选择后立即更新CountryComboBox

我尝试使用EventTriggers进行实验,但我不确定应该听哪个事件和哪个控件来获取Selecteditem的{​​{1}}。

非常感谢任何建议或链接。

谢谢

修改

使用CountryComboBox CommandParameter EventTrigger内的CountryComboBox属性似乎对我有用。

public ICommand CountryComboBoxItemSelectedCommand { get; set; }
CountryComboBoxItemSelectedCommand = new 
                                  RelayCommand(UpdateCityComboBoxItemsSource);

和xaml

<ComboBox x:Name="CountryComboBox"
    ItemsSource="{Binding Path=DataContext.Countries,
        RelativeSource={RelativeSource AncestorType=Custom:CustomDataGrid}}"
    SelectedItem="{Binding Path=Country, Mode=TwoWay}"
    DisplayMemberPath="Name">
    <i:Interaction.Triggers>
      <i:EventTrigger EventName="SelectionChanged">
        <i:InvokeCommandAction Command="{Binding
          Path=DataContext.CountryComboBoxItemSelectedCommand,
          RelativeSource={RelativeSource AncestorType=Custom:CustomDataGrid}}"
          CommandParameter="{Binding SelectedItem,ElementName=CountryComboBox}"/>
      </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>

1 个答案:

答案 0 :(得分:1)

我这么做了很多次。

我以前做的是在XAML中为Country组合框创建一个SelectedItem属性。

假设,SelectedItem属性的名称是&#34; SelectedCountry&#34;。现在转到此属性的Setter并将一个Itemsource分配给MVVM中的city combobox。

private Country _SelectedCountry;
        public Country SelectedCountry
        {
            get
            {
                return _SelectedCountry;
            }
            set
            {
                if (_SelectedCountry!= value)
                {
                    _SelectedCountry= value;

                    AllCities = get this from your datasource.
                    RaisePropertyChanged("SelectedCountry");
                }
            }

        }

希望这会对你有帮助..!

快乐学习!! !!