【发布时间】:2015-05-11 07:07:27
【问题描述】:
我正在使用 MVVM 模式开发 WPF 应用程序。我在视图中有一个组合,在视图模型(项目和组织)中有两个列表。根据组织列表项,我必须绑定组织名称。 例如,如果组织列表的 Count 属性为 1,则组合框项必须为 "ProjectName",如果组织列表的 Count 属性大于 1,则组合框项应类似于 >“项目名称 - 组织名称”。 这是我拥有的 XAML 代码:
<ComboBox x:Name="textBox3" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" ItemsSource="{Binding Path=Projects}" DisplayMemberPath="Name" IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding Path=SelectedProject}">
</ComboBox>
我应该如何实现这个目的。我希望能有一点帮助。干杯。
我在视图模型中添加了属性 projectFullName 但我得到了一个空的组合框:
public string ProjectFullName
{
get
{
if (this.organizations.ToList().Count > 1)
{
this.projectFullName = string.Format("{}{0} - {1}", this.selectedProject.Name, this.organizations.First(org => org.Id == this.selectedProject.OrganizationId).Name);
}
else if (this.organizations.ToList().Count == 1)
{
this.projectFullName = this.selectedProject.Name;
}
return this.projectFullName;
}
}
XAML 代码:
<ComboBox x:Name="textBox3" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" ItemsSource="{Binding Path=Projects}" DisplayMemberPath="{Binding Path=ProjectFullName}" IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding Path=SelectedProject}">
</ComboBox>
【问题讨论】:
-
你需要使用datatemplate和datatriggers
-
但是怎么用呢,能给我举个例子吗
-
google 是你的朋友,那里有很多例子