gpt4 book ai didi

c# - 将组合框绑定(bind)到模型/ View 模型

转载 作者:太空宇宙 更新时间:2023-11-03 10:23:35 25 4
gpt4 key购买 nike

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

这是我的两个表: The attribute the client_typeID is linked to is called 'ClientType'. client_type1 is what i want the View to display

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

这是我用于获取客户端类型列表的 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,它具有正确的 ItemSource 和选择的正确值。唯一的问题是,当我更改 SelectedItem 时,它会在运行 Update 方法时更新 Client_Type 表的描述,而不是 Entity 表的外键。

我也试着关注this示例,但是它对我没有帮助。

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

最佳答案

实际上组合框没有 ID 信息,您还必须在 VM 中绑定(bind) SelectedValue 或 SelectedItem 属性。您应该在 View 模型中创建 Client_Type 列表,然后将其与 View 绑定(bind)。我做了一个小的 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.希望这些信息有所帮助。

关于c# - 将组合框绑定(bind)到模型/ View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32453824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com