c# - MVVM WPF 一项来源两个组合框不同的属性,第二个属性最初没有设置
问题描述
目前,当我将两个组合框绑定到同一个项目源时,我将组合框的 SelectedItem 设置为两个单独的属性,并将组合框的 SelectedIndex 设置为两个不同的索引
UI 加载后组合框加载并显示正确的索引但是,在 viewModel 中设置了 Person 的属性,但没有设置 PersonTwo 的属性,直到我从第二个组合框中手动选择不同的项目。
如果我然后创建一个单独但相同的列表并将第二个组合框的 itemsource 绑定到它,则 PersonTwo 的数据设置为 selectedIndex
我试图搜索以更好地了解正在发生的事情,但我离找到答案还差得远。我不明白为什么当两个组合框都使用相同的项目源时会出现这种情况。
最小的工作示例:模型
namespace WpfApp1
{
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
}
视图模型
using System.Collections.Generic;
namespace WpfApp1
{
public class GenericViewModel
{
public GenericViewModel()
{
People = new List<Person>();
Person personToMakeList = null;
People.Add(personToMakeList = new Person(){Age = 10,Id=1,Name="John"});
People.Add(personToMakeList = new Person() { Age = 20, Id = 2, Name = "Amber" });
People.Add(personToMakeList = new Person() { Age = 30, Id = 3, Name = "Amy" });
People.Add(personToMakeList = new Person() { Age = 40, Id = 4, Name = "Joey" });
Person = new Person();
PersonTwo = new Person();
}
public Person Person { get; set; }
public List<Person> People { get; set; }
public Person PersonTwo { get; set; }
}
}
看法
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.DataContext>
<local:GenericViewModel/>
</Grid.DataContext>
<StackPanel Orientation="Vertical">
<StackPanel>
<ComboBox ItemsSource="{Binding People}"
SelectedIndex="0"
SelectedItem="{Binding Person}"
DisplayMemberPath="Name"/>
</StackPanel>
<StackPanel Orientation="Vertical">
<TextBlock Text="Person One"></TextBlock>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Age: "/>
<TextBlock Text="{Binding Person.Age}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Id: "/>
<TextBlock Text="{Binding Person.Id}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Name: "/>
<TextBlock Text="{Binding Person.Name}"/>
</StackPanel>
</StackPanel>
<StackPanel>
<ComboBox ItemsSource="{Binding People}"
SelectedIndex="3"
SelectedItem="{Binding PersonTwo}"
DisplayMemberPath="Name"/>
</StackPanel>
<StackPanel Orientation="Vertical">
<TextBlock Text="Person Two"></TextBlock>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Age: "/>
<TextBlock Text="{Binding PersonTwo.Age}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Id: "/>
<TextBlock Text="{Binding PersonTwo.Id}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Name: "/>
<TextBlock Text="{Binding PersonTwo.Name}"/>
</StackPanel>
</StackPanel>
</StackPanel>
</Grid>
解决方案
推荐阅读
- sql - 将单列日期列表转换为 SQL Server 中的多个日期范围
- c++ - 这个工厂函数有什么问题?
- oracle - 如何在 Oracle 过程中创建临时表/视图?
- sql - Laravel 查询生成器:这个命令是否可以免除 SQL 注入?
- c - 使用 C11 GCC 使数据读/写原子化
? - sql - 如果语句包含没有 INTO 子句的 OUTPUT 子句,则 DML 语句不能有任何启用的触发器
- java - 使用 EasyMock 在 junit 测试用例中跳过方法调用?
- authentication - DNN 9 Persona Bar 登录后不可见
- html - iPhone上的页脚超出屏幕
- bash - Bash:回显文件内容以计数器开头