【问题标题】:Binding a ComboBox to the Model/ViewModel将 ComboBox 绑定到模型/视图模型
【发布时间】:2015-09-08 09:16:42
【问题描述】:

我正在尝试做的是将ItemSource 绑定到我模型中的表中的所有值,并将SelectedValue 绑定到第一个链接到的表的外键。以下是我尝试过的。

这是我的两张桌子:

注意:client_typeID 链接到的属性称为“ClientType”。 client_type1 是我希望视图显示的内容。

这是我获取客户端类型列表的 DataAccessService 方法:

public List<string> GetClientTypes()
{
    List<string> ClientType = new List<string>();
    foreach (var item in context1.Client_Type)
    {
        ClientType.Add(item.client_type1);
    }
    return ClientType;
}

这是我的 ViewModel:

public List<string> Client_type_list
{
    get { return ServerPxy.GetClientTypes(); }
}

private Entity record;
public Entity Record
{
    get { return record; }
    set
    {
        record = value;
        RaisePropertyChanged("Record");
    }
}

注意:ServerPxy 是我的 DataAccessService 实例,Record 是实体表的属性。

这是我的 ComboBox 的 XAML:

<ComboBox Grid.Column="3" Grid.Row="0" MaxHeight="26" Margin="2" ItemsSource="{Binding Client_type_list}" Text="{Binding Record.Client_Type.client_type1}" />

所有这些都为我提供了ComboBox,该ComboBox 具有正确的ItemSource 和选择的正确值。唯一的问题是,当我更改 SelectedItem 时,它会在运行 Update 方法时更新 Client_Type 表的描述,而不是 Entity 表的外键。

我也尝试遵循this 的示例,但它对我没有帮助。

如果有任何帮助,我将不胜感激。

【问题讨论】:

  • 还请看一下这个回复:stackoverflow.com/a/4902454/4424024 它解释了在 ItemControls 上使用绑定时 SelectedItem、SelectedValue 和 SelectedValuePath 之间的区别,例如 ComboBox

标签: c# wpf mvvm combobox


【解决方案1】:

实际上组合框没有 ID 信息,您也必须在 VM 中绑定 SelectedValue 或 SelectedItem 属性。您应该在视图模型中创建 Client_Type 列表,然后将其与视图绑定。我做了一个小的 POC,希望这会给你足够的信息。你的代码看起来像

查看

 <ComboBox Grid.Column="3" Grid.Row="0" MaxHeight="26" Margin="2" ItemsSource="{Binding Client_type_list}" 
                  ItemTemplate="{StaticResource Client_typeDataTemplate}"
                  SelectedValue="{Binding Record.Client_Type}">
 <ComboBox.Resources>
 <DataTemplate x:Key="Client_typeDataTemplate">
 <TextBlock Text="{Binding Client_type1} "/>
  </DataTemplate>
   </ComboBox.Resources>
  </ComboBox>
        <Button Click="Button_Click" Height="20"/>

ViewModel(假设您知道这一点,我已将代码背后的代码与 viewmodel 合并)

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        Record = new Entity();
    }

    public List<Client_Type> Client_type_list
    {
        get { return GetClientTypes(); }
    }

    private Entity record;
    public Entity Record
    {
        get { return record; }
        set
        {
            record = value;
            OnPropertyChanged("Record");
        }
    }


    protected void OnPropertyChanged(string propertyName)
    {
        var handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;


    public List<Client_Type> GetClientTypes()
    {
        List<Client_Type> ClientType = new List<Client_Type>();
        {
            ClientType.Add(new Client_Type() { Client_type1 = "a", Client_typeId = "1" });
            ClientType.Add(new Client_Type() { Client_type1 = "b", Client_typeId = "2" });
        }
        return ClientType;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(Record.Client_Type.Client_typeId);
    }

}

Now when the selection is changing of the Record is also changing because of binding.希望这些信息对您有所帮助。

【讨论】:

  • 感谢您的回复,您能否进一步解释一下 ItemTemplate="{StaticResource Client_typeDataTemplate}"。谢谢!
  • 这是一个数据模板。请查看更新的答案。
猜你喜欢
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-31
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多