简体   繁体   English

如何在ListView中将两列的值连接成一个

[英]How to concatenate the values of two Columns into One inside ListView

I have a .xaml file which has a listview. 我有一个具有列表视图的.xaml文件。 Listview has 2 items inside which are bind in a following way: 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>

Both Description and Devicename are part of ModelClass.In My ViewModel class I am able to extract the Device name as well as Description from the hardware I have connected. DescriptionDevicename都是ModelClass的一部分。在My ViewModel类中,我可以从连接的硬件中提取Device名称以及Description。

    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();            
    }

Here ComDeviceManagement is my class which has FINDDevices() which returns me the devicename and description. 这里的ComDeviceManagement是我的类,具有FINDDevices() ,它向我返回设备名称和描述。 U can notice DeviceList = ComDeviceManagement.FindDevices() above which indicates both the descrip and name are present inside the list. U可以注意到DeviceList = ComDeviceManagement.FindDevices()上面的内容指示列表中同时存在描述和名称。

Everything is working fine. 一切正常。 But What I basically want is to display both the Devicename and Description in One Column rather than two separate columns. 但是我基本上想要的是在一列而不是两列中同时显示设备名称和描述。 Well the problem I am facing here is with Devicename and Description. 好吧,我在这里面临的问题是设备名称和描述。 Even though they both display different values, Isn't their a way where I can concatinate them and display both the values into a single Column??? 即使它们都显示不同的值,我也不能用它们来合并它们,并将两个值都显示在一个列中吗??? You may notice another column in .xaml file but I want to display(concatenate) both these inside my single column in listView. 您可能会注意到.xaml文件中的另一列,但是我想在listView的我的单个列中显示(连接)这两者。 How can i do that? 我怎样才能做到这一点?

Please help!! 请帮忙!!

Use a MultiBinding with a format string. 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>

The thing you have to understand with a MultiBinding is that if the target property is not a string then you must provide a converter. 使用MultiBinding必须了解的是,如果target属性不是字符串,则必须提供一个转换器。 If it is a string , you can get away with just using the format string. 如果它是一个字符串 ,则可以只使用格式字符串。

So, in your case, you can't use it (easily) via the DisplayMemberBinding, you have to specify the content as in my example above. 因此,就您而言,您不能通过DisplayMemberBinding(轻松)使用它,您必须像上面的示例中那样指定内容。

Two approaches 两种方法

In the ComDeviceInfo class just add a property that concatenates 在ComDeviceInfo类中,只需添加一个串联的属性

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

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

Or use a multi-value converter 或使用多值转换器

MultiBinding.Converter MultiBinding.Converter

Will provided and example. 将提供和示例。

I would add a new property, but don't forget to update it too when its components change. 我会添加一个新属性,但是当其组件发生更改时也不要忘记对其进行更新。

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

If you are using LINQ query to get your data you can do this: 如果您正在使用LINQ查询来获取数据,则可以执行以下操作:

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

listview.ItemsSource = query;

Then go into the GridView.Column and use: 然后进入GridView.Column并使用:

DisplayMemberBinding="{Binding DeviceDesc}" 

and it will bind the concatenated column you just created. 它将绑定刚创建的串联列。 Obviously you will need to write the correct query, mine was just an example to go by... 显然,您将需要编写正确的查询,我只是一个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM