WPF:在同一列中带有复选框和文本框的列表视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1830501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:17:22  来源:igfitidea点击:

WPF: listview with checkbox and textbox in the same column

wpflistviewcheckboxtextbox

提问by Warpin

I'd like to accomplish the following listview, which uses different controls in the same column.

我想完成以下列表视图,它在同一列中使用不同的控件。

It has two columns: Name and Setting. The first row's entry in the Name column is "On/Off" and the Setting is a checkbox. The second row's Name is "Elevation" and its setting is a textbox.

它有两列:名称和设置。名称列中第一行的条目是“开/关”,设置是一个复选框。第二行的名称是“海拔”,其设置是一个文本框。

I want to be able to populate the listview programmatically.

我希望能够以编程方式填充列表视图。

Thanks!

谢谢!

回答by Wallstreet Programmer

Template selectors allow you at runtime to select between different data templates to use in items controls like list view.

模板选择器允许您在运行时在不同的数据模板之间进行选择,以在列表视图等项目控件中使用。

XAML:

XAML:

<Window x:Class="ListViewTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ListViewTest"
    Height="300" Width="300">

    <Window.Resources>
        <DataTemplate x:Key="textBoxTemplate">
            <TextBox Text="{Binding Path=Value}" Width="100"></TextBox>
        </DataTemplate>

        <DataTemplate x:Key="checkBoxTemplate">
            <CheckBox IsChecked="{Binding Path=Value}" IsThreeState="False"></CheckBox>
        </DataTemplate>

        <local:SettingsTemplateSelector 
            x:Key="settingsTemplateSelector" 
            TextBoxTemplate="{StaticResource textBoxTemplate}" 
            CheckBoxTemplate="{StaticResource checkBoxTemplate}" />

    </Window.Resources>

    <ListView ItemsSource="{Binding Path=Settings}">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn 
                        Header="Name" 
                        DisplayMemberBinding="{Binding Path=Name}" />                          
                    <GridViewColumn 
                        Header="Setting" 
                        CellTemplateSelector="{StaticResource settingsTemplateSelector}" />
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>

</Window>

Code behind:

后面的代码:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace ListViewTest
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            Settings = new List<Setting>();
            Settings.Add(new Setting("On/Off", true));
            Settings.Add(new Setting("Elevation", "100"));

            DataContext = this;
        }

        public List<Setting> Settings { get; private set; }
    }

    public class Setting
    {
        public Setting(string name, string value)
        {
            Name = name;
            Value = value;
            IsCheckBox = false;
        }

        public Setting(string name, bool value)
        {
            Name = name;
            Value = value;
            IsCheckBox = true;
        }

        public string Name { get; private set; }
        public object Value { get; set; }
        public bool IsCheckBox { get; private set; }
    }

    public class SettingsTemplateSelector : DataTemplateSelector
    {
        public DataTemplate CheckBoxTemplate { get; set;}
        public DataTemplate TextBoxTemplate { get; set;}

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            Setting setting = item as Setting;
            if (setting.IsCheckBox)
            {
                return CheckBoxTemplate;
            }
            return TextBoxTemplate;
        }
    }
}

回答by serge_gubenko

Please check an example below, it should give you an idea on how to proceed

请检查下面的示例,它应该可以让您了解如何进行

xaml

xml

    <ListView x:Name="checkList" Height="100" Margin="129,168,187,43">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <CheckBox Content="name" IsChecked="{Binding Checked, Mode=TwoWay}" />
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Elevation" />
                        <TextBox Text="{Binding Text}" />
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

you can bind it to the list of objects with Checked and Text properties. Below is an example:

您可以将其绑定到具有 Checked 和 Text 属性的对象列表。下面是一个例子:

        public class CheckBoxListItem
        {
            public bool Checked { get; set; }
            public string Text { get; set; }

            public CheckBoxListItem(bool ch, string text)
            {
                Checked = ch;
                Text = text;
            }
        }

<...>

List<CheckBoxListItem> items1 = new List<CheckBoxListItem>();
items1.Add(new CheckBoxListItem(true, "test1"));
items1.Add(new CheckBoxListItem(false, "test2"));
checkList.ItemsSource = items1;

hope this helps, regards

希望这有帮助,问候

回答by Subindev

<ListView Name="listView">
 <ListView.View>
     <GridView>
           <GridViewColumn  Header="Name">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Name}" Width="Auto"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
             </GridViewColumn>
        <GridViewColumn  Header="Settings" >
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                  <StackPanel>
                      <CheckBox Text="{Binding Path=CheckProperty}" Width="Auto" />
                       <TextBox Text="{Binding Path=TextProperty}" Width="Auto" />
                   </StackPanel>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>
    </GridView>

 </ListView.View>

</ListView >

.. Make gridview as ur listview's default view. and dothe rest using Gridviewcolumn data templates..

.. 将 gridview 作为你的 listview 的默认视图。其余使用 Gridviewcolumn 数据模板。

then bind ur data source..

然后绑定你的数据源..