2010-08-12 104 views
3

我正在学习WPF mvvm,并且一直在挣扎,我觉得它可能很简单,但一直无法自行解决。根据另一个选择填充一个组合框

我想要的是能够选择填充组合框中的项目,然后根据该选择填充另一个组合框。我似乎无法获得响应选择加载的第二个组合框。

我已经包含了一个2个组合框和一个文本框的例子。当我运行应用程序并在第一个组合框中选择一个项目时,将根据绑定更新文本块,但我不知道在哪里调用代码来更新第二个组合框。

我试着将调用添加到SelectedString属性设置器,但似乎永远不会被调用。我确信我错过了一些简单的东西,但我需要有人帮助揭开面纱!

我试过其他帖子上的建议,但到目前为止我一直没有成功。

下面是该视图的XAML:

<Window x:Class="ThrowAwayMVVMApp.Views.MainView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Main Window" Height="400" Width="800"> 

    <DockPanel> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="60*" /> 
       <RowDefinition Height="282*" /> 
      </Grid.RowDefinitions> 
      <!-- Add additional content here --> 
      <ComboBox ItemsSource="{Binding MyStrings}" SelectedItem="{Binding Path=SelectedString, Mode=TwoWay}" Height="23" HorizontalAlignment="Left" Margin="18,24,0,0" Name="comboBox1" VerticalAlignment="Top" Width="204" /> 
      <TextBlock Text="{Binding SelectedString}" Height="23" HorizontalAlignment="Left" Margin="276,24,0,0" Name="textBlock1" VerticalAlignment="Top" Width="227" Background="#FFFAE7E7" /> 
      <ComboBox ItemsSource="{Binding ResultStrings}" Height="23" HorizontalAlignment="Left" Margin="543,25,0,0" Name="comboBox2" VerticalAlignment="Top" Width="189" /> 
     </Grid> 
    </DockPanel> 
</Window> 

这里是视图模型:

public class MainViewModel : ViewModelBase 
{ 
    public MainViewModel() 
    { 
     this.MyStrings = new ObservableCollection<string> 
      { 
       "One", 
       "Two", 
       "Three", 
       "Four", 
       "Five" 
      }; 
    } 

    public ObservableCollection<string> MyStrings { get; set; } 
    public ObservableCollection<string> ResultStrings { get; set; } 

    public string SelectedString 
    { 
     get { return (string)GetValue(SelectedStringProperty); } 
     set 
     { 
      SetValue(SelectedStringProperty, value); 
      this.ResultStrings = getResultStrings(value); 
     } 
    } 

    // Using a DependencyProperty as the backing store for SelectedString. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty SelectedStringProperty = 
     DependencyProperty.Register("SelectedString", typeof(string), typeof(MainViewModel), new UIPropertyMetadata("")); 


    private ObservableCollection<string> getResultStrings(string input) 
    { 
     ObservableCollection<string> result = null; 

     if (input == "Three") 
     { 
      result = new ObservableCollection<string> { "Six", "Seven", "Eight" }; 
     } 
     else 
     { 
      result = new ObservableCollection<string> { "Nine", "Ten", "Eleven" }; 
     } 

     return result; 
    } 
} 
+0

,为什么你有一个DP您的视图模型,通常它的意思内宣布你是绑定的控件中去至? – Agies 2010-08-13 10:55:44

回答

2

的SelectedString依赖属性的实现是错误的:要注册PropertyChangedCallback时得到通知的DP直接访问而不是CLR属性集(请参阅http://msdn.microsoft.com/en-us/library/system.windows.propertychangedcallback.aspx);这样,即使直接使用DP,您也可以更改相关收集。

当绑定依赖项属性(如SelectedString)时,WPF绑定不使用CLR属性设置器,因此您永远不会调用getResultStrings。顺便说一下,我会考虑在视图模型上使用POCO方法,实现INotifyPropertyChanged:DP是一种很痛苦的写法,并且给虚拟机添加了很多噪声(除了对System.Windows的恶意依赖)之外。

看一看这个博客帖子的广泛的比较:http://kentb.blogspot.com/2009/03/view-models-pocos-versus.html

0

尝试

private string selectedString; 
public string SelectedString 
{ 
    get { return selectedString; } 
    set 
    { 
     if (selectedString == value) return; 
     selectedString = value; 
     // Required for the UI to know the change was successful 
     RaisePropertyChanged("SelectedString"); 
     LoadStringResults(value); 
    } 
} 

private ObservableCollection<string> resultStrings; 
public ObservableCollection<string> ResultStrings 
{ 
    get { return resultStrings; } 
    set 
    { 
     if (resultStrings== value) return; 
     resultStrings= value; 
     // Required for databinding to know that ResultStrings changed 
     // Previously you changed this property without updating the UI 
     RaisePropertyChanged("ResultStrings"); 
    } 
} 

private void LoadStringResults(string input) 
{ 
    ObservableCollection<string> result = null; 

    if (input == "Three") 
    { 
     result = new ObservableCollection<string> { "Six", "Seven", "Eight" }; 
    } 
    else 
    { 
     result = new ObservableCollection<string> { "Nine", "Ten", "Eleven" }; 
    } 

    ResultStrings = result; 
} 
相关问题