0

我有一个包含列表视图的 .xaml 文件。Listview 有 2 个项目,其中以下列方式绑定:

<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>

描述和设备名称都是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() 表明描述和名称都存在于列表中。

一切正常。但我基本上想要的是在一列中显示设备名称和描述,而不是两个单独的列。那么我在这里面临的问题是设备名称和描述。即使它们都显示不同的值,难道不是我可以将它们连接起来并将两个值显示到单个列中的一种方式吗???您可能会注意到 .xaml 文件中的另一列,但我想在 listView 的单个列中显示(连接)这两个列。我怎样才能做到这一点?

请帮忙!!

4

4 回答 4

5

使用带有格式字符串的MultiBinding 。

<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>

对于 MultiBinding,您必须了解的是,如果目标属性不是字符串,那么您必须提供一个转换器。如果它是一个字符串,你可以只使用格式字符串。

因此,在您的情况下,您不能(轻松地)通过 DisplayMemberBinding 使用它,您必须像上面的示例中那样指定内容。

于 2012-09-27T12:43:33.147 回答
4

两种方法

在 ComDeviceInfo 类中,只需添加一个连接属性

  public string DescName 
  {
      get 
      {
           return Description + " " + Name;
      }
  {

<GridViewColumn Width="300" Header="Name" DisplayMemberBinding="{Binding DescName}" />

或者使用多值转换器

MultiBinding.Converter

将提供和示例。

于 2012-09-27T12:25:38.807 回答
2

我会添加一个新属性,但不要忘记在其组件更改时也对其进行更新。

 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; }
 }
于 2012-09-27T12:44:04.620 回答
0

如果您使用 LINQ 查询来获取数据,您可以这样做:

var query = from devices in DeviceList
            select new
            {.DeviceDesc = devices.device + " " devices.desc}

listview.ItemsSource = query;

然后进入 GridView.Column 并使用:

DisplayMemberBinding="{Binding DeviceDesc}" 

它将绑定您刚刚创建的连接列。显然你需要编写正确的查询,我的只是一个例子......

于 2016-09-06T05:52:23.727 回答