C# 如何在ListView中将两列的值连接成一列
我有一个.xaml文件,它有一个listview。Listview中有2个项,它们以以下方式绑定:C# 如何在ListView中将两列的值连接成一列,c#,wpf,listview,mvvm,C#,Wpf,Listview,Mvvm,我有一个.xaml文件,它有一个listview。Listview中有2个项,它们以以下方式绑定: <ListView Name="listView" ItemsSource="{Binding DeviceList}" SelectedItem="{Binding ConnectedDevice, Mode=TwoWay}" > <ListView.View> <GridView> &
<ListView Name="listView" ItemsSource="{Binding DeviceList}" SelectedItem="{Binding ConnectedDevice, Mode=TwoWay}" >
<ListView.View>
<GridView>
<GridViewColumn Width="300" Header="Name" DisplayMemberBinding="{Binding Description}" />
<GridViewColumn Width="240" Header="Connection Status" DisplayMemberBinding="{Binding DeviceName}" />
</GridView>
</ListView.View>
</ListView>
Description和Devicename都是ModelClass的一部分。在我的ViewModel类中,我能够从连接的硬件中提取设备名称和描述
public ObservableCollection<ComDeviceInfo> DeviceList
{
get { return comDevices; }
set
{
comDevices = value;
NotifyPropertyChanged("DeviceList");
}
}
public ComDeviceInfo ConnectedDevice
{
get { return connectedDevice; }
set
{
connectedDevice = value;
NotifyPropertyChanged("ConnectedDevice");
}
}
//Called Inside Constructor
private void checkForDevicesTimer_Elapsed(Object source, ElapsedEventArgs e)
{
DeviceList = ComDeviceManagement.FindDevices();
}
这里,ComDeviceManagement是我的类,它有FINDDevices,返回设备名称和描述。您可以注意到上面的DeviceList=ComDeviceManagement.FindDevices,这表明描述符和名称都存在于列表中
一切正常。但我基本上希望在一列中显示Devicename和Description,而不是在两个单独的列中。我现在面临的问题是设备名称和描述。即使它们都显示不同的值,难道它们不是一种我可以将它们合并并将两个值显示在一列中的方式吗???您可能会注意到.xaml文件中的另一列,但我想在listView中的单个列中显示并连接这两个列。 我该怎么做 请帮忙 两种方法 在ComDeviceInfo类中,只需添加一个连接
public string DescName
{
get
{
return Description + " " + Name;
}
{
<GridViewColumn Width="300" Header="Name" DisplayMemberBinding="{Binding DescName}" />
或者使用多值转换器
将提供和示例。将a与格式字符串一起使用
<GridViewColumn>
<TextBlock>
<!-- the Text property is of type System.String -->
<TextBlock.Text>
<MultiBinding StringFormat="{0} {1}">
<Binding Path="Description "/>
<Binding Path="Name"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</GridViewColumn>
使用多重绑定必须了解的一点是,如果目标属性不是字符串,那么必须提供转换器。如果它是一个字符串,您可以不使用格式字符串
<GridViewColumn>
<TextBlock>
<!-- the Text property is of type System.String -->
<TextBlock.Text>
<MultiBinding StringFormat="{0} {1}">
<Binding Path="Description "/>
<Binding Path="Name"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</GridViewColumn>
因此,在您的情况下,您无法通过DisplayMemberBinding轻松地使用它,您必须像上面的示例中那样指定内容。我会添加一个新属性,但不要忘记在其组件更改时也更新它
private ComDeviceInfo _model;
public string DeviceName
{
get { return _model.Name; }
set
{
_model.Name = value;
NotifyPropertyChanged("DeviceName");
NotifyPropertyChanged("Combined");
}
}
public string Description
{
get { return _model.Description; }
set
{
_model.Description = value;
NotifyPropertyChanged("Description");
NotifyPropertyChanged("Combined");
}
}
public string Combined
{
get { return string.Format("{0} : {1}", _description, _deviceName; }
}
如果使用LINQ查询获取数据,可以执行以下操作:
var query = from devices in DeviceList
select new
{.DeviceDesc = devices.device + " " devices.desc}
listview.ItemsSource = query;
然后进入GridView.Column并使用:
DisplayMemberBinding="{Binding DeviceDesc}"
它将绑定刚刚创建的连接列。显然,您需要编写正确的查询,我的只是一个示例…我在这里面临的问题是Devicename和Description。即使它们都显示不同的值,难道它们不是一种我可以将它们合并并将它们的值显示到一个列中的方法吗???为什么不创建一个属性,将它们内部合并,并将其绑定到一个列中?不知道如何做:嘿,怪,你的第一种方法似乎很简单。你能详细说明一下吗?正如如何访问视图模型中的每个描述和名称一样,Descname必须与xaml中的某些内容绑定,等等。。。。你能提供一个示例代码吗?:^我没有使用字符串。我正在使用ComdDeviceInfo从模型类中检索名称和描述。还有别的办法吗数据存储在哪里并不重要。