【发布时间】:2010-12-22 06:39:23
【问题描述】:
我想完成以下列表视图,它在同一列中使用不同的控件。
它有两列:名称和设置。 Name 列中第一行的条目是“On/Off”,而 Setting 是一个复选框。第二行的名称是“海拔”,它的设置是一个文本框。
我希望能够以编程方式填充列表视图。
谢谢!
【问题讨论】:
标签: wpf listview checkbox textbox
我想完成以下列表视图,它在同一列中使用不同的控件。
它有两列:名称和设置。 Name 列中第一行的条目是“On/Off”,而 Setting 是一个复选框。第二行的名称是“海拔”,它的设置是一个文本框。
我希望能够以编程方式填充列表视图。
谢谢!
【问题讨论】:
标签: wpf listview checkbox textbox
模板选择器允许您在运行时在不同的数据模板之间进行选择,以在列表视图等项目控件中使用。
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;
}
}
}
【讨论】:
as 调用可能会返回空值。
<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 作为你的 listview 的默认视图。其余的使用 Gridviewcolumn 数据模板..
然后绑定你的数据源..
【讨论】:
请查看下面的示例,它应该可以让您了解如何进行
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;
希望这会有所帮助, 问候
【讨论】: