WPF:listview,同一列中包含复选框和文本框

时间:2009-12-02 03:33:24

标签: wpf listview checkbox textbox

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

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

我希望能够以编程方式填充listview。

谢谢!

3 个答案:

答案 0 :(得分:5)

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

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>

代码背后:

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

答案 1 :(得分:1)

请查看下面的示例,它应该让您知道如何继续

XAML

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

您可以将其绑定到具有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;

希望这有帮助, 问候

答案 2 :(得分:1)

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

..将gridview作为ur listview的默认视图。并使用Gridviewcolumn数据模板进行休息。

然后绑定你的数据源..