我将使用listview显示数据列表,该数据类包含以下元素:
class MyData
{
static Random rnd = new Random();
public int id { get; set; }
public string name { get; set; }
public bool selected { get; set; }
private LED[] LEDs = new LED[21];
private byte[] servos = new byte[21];
}
并希望以如下表格式显示数据,
{selected} {id} {name} {servo[1]} {servo[2]} ......
(servo [0]应该被隐藏)
每列的布局:
- {selected] : checkbox
- {id} : right alignment
- {name] : left alignment
- {servo} : right alignment, with background based on LED setting
我已经建立了ListView如下
<ListView x:Name="lvData" FontSize="13" Background="LightGoldenrodYellow" Margin="0,0,0,0" >
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn >
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding selected}" Checked="Selection_Changed" Unchecked="Selection_Changed" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="id" Width="60">
<GridViewColumn.CellTemplate>
<DataTemplate >
<TextBlock Text="{Binding id}" TextAlignment="Right" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="name" Width="100" DisplayMemberBinding="{Binding name}"/>
<GridViewColumn Header="s01" Width="35">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid Background="{Binding Path=LED01}" Margin="-5,0,-5,0">
<Label Margin="0,0,0,0" Content="{Binding Path=S01}" HorizontalContentAlignment="Right"></Label>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="s02" Width="35">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid Background="{Binding Path=LED02}" Margin="-5,0,-5,0">
<Label Margin="0,0,0,0" Content="{Binding Path=S01}" HorizontalContentAlignment="Right"></Label>
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
:
{repeat for s03...s20}
:
</GridView>
</ListView.View>
</ListView>
在我的数据类中添加了一些属性:
public string LED01 { get { return GetLED(1); } }
public string S01 { get { return GetServo(1); } }
public string LED02 { get { return GetLED(2); } }
public string S02 { get { return GetServo(2); } }
两种方法GetLED和GetServo根据给定索引的阵列LED和伺服器的值返回颜色和显示。
我多次重复执行GridViewColumn块似乎很伪,并且已经创建了40个伪属性,因为它无法直接绑定到方法。
我可以知道是否有任何简单的方法来构建列表视图吗?
实际上,数组的大小不是固定的,21只是最大大小。
我想知道它是否可以动态构建列,以便可以根据实际大小创建列,并且可以在循环内完成重复的列。并根据类方法设置背景和数据内容,这样我就可以直接将其设置为GetLED(?)和GetServo(?),而无需为其构建虚拟属性。
提前致谢。
您的问题有点冗长且合乎逻辑。我试图实现您所需的功能。
请抛出以下内容:
楷模
我将LED和Servo详细信息划分为另一个类,并将该类的List对象划分为MyData类。
class MyData
{
public int id { get; set; }
public string name { get; set; }
public bool selected { get; set; }
public List<MySubData> lstSubData { get; set; }
}
class MySubData
{
public string LED;
public string Servo;
}
转换器
在此过程中,我们需要两个转换器。
在ColorConverter中,您需要定义根据LED值生成不同颜色的条件。
public class TextConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return System.Convert.ToString(value).Split('#').First();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new NotImplementedException();
}
}
public class ColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var val = System.Convert.ToString(value).Split('#').Last();
//YOUR COLOR CONDITIONS HERE. I AM RETURNING SIMPLE ONE COLOR
return new SolidColorBrush(Colors.Red);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new NotImplementedException();
}
}
XAML
<Window x:Class="WPFTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFTest"
mc:Ignorable="d"
Title="TestWPF" Height="300" Width="400"
WindowStyle="SingleBorderWindow"
WindowStartupLocation="CenterScreen">
<Grid>
<ListView x:Name="lvData" FontSize="13" Background="LightGoldenrodYellow" Margin="0,0,0,0" >
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Grid>
</Window>
代码背后
我应用的主要逻辑在(C#)之后的代码中。我仅在CS页面中生成ListView.View。此代码将为UI和数据生成动态GridView和DataTable。
我采用了样本数据来模拟您的需求。您可以获取实际数据。我使用了变量globalServo来定义列数的设置(如您在注释中所述)。
public partial class MainWindow : Window
{
public int globalServo = 21;
public MainWindow()
{
InitializeComponent();
List<MyData> lstMyData = new List<MyData>();
for (int i = 1; i < 6; i++)
{
List<MySubData> lstMySubData = new List<MySubData>();
for (int j = 0; j < globalServo; j++)
{
lstMySubData.Add(new MySubData() { LED = "LED" + j, Servo = "Servo" + j });
}
lstMyData.Add(new MyData()
{
id = i,
name = "name-" + i,
selected = Convert.ToBoolean(i % 2),
lstSubData = lstMySubData
});
}
lvData.View = GenerateGridView();
lvData.ItemsSource = GenerateSource(lstMyData).DefaultView;
}
private GridView GenerateGridView()
{
GridView view = new GridView();
view.Columns.Add(new GridViewColumn() { Header = "Id", DisplayMemberBinding = new Binding("Id") });
view.Columns.Add(new GridViewColumn() { Header = "Name", DisplayMemberBinding = new Binding("Name") });
view.Columns.Add(new GridViewColumn() { Header = "Selected", CellTemplate = GetCheckboxTemplate() });
for (int i = 1; i <= globalServo; i++)
{
view.Columns.Add(new GridViewColumn() { Header = "Servo" + i, CellTemplate = GetTextBlockTemplate(i) });
}
return view;
}
private DataTable GenerateSource(List<MyData> dataList)
{
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Name");
dt.Columns.Add("Selected");
for (int i = 1; i <= globalServo; i++)
{
dt.Columns.Add("Servo" + i);
}
foreach (var item in dataList)
{
DataRow row = dt.NewRow();
row["Id"] = item.id;
row["Name"] = item.name;
row["Selected"] = item.selected;
for (int i = 1; i <= globalServo; i++)
{
row["Servo" + i] = item.lstSubData[i - 1].Servo + "##" + item.lstSubData[i - 1].LED;
}
dt.Rows.Add(row);
}
return dt;
}
private DataTemplate GetCheckboxTemplate()
{
DataTemplate dt = new DataTemplate(typeof(CheckBox));
FrameworkElementFactory chkElement = new FrameworkElementFactory(typeof(CheckBox));
dt.VisualTree = chkElement;
Binding bind = new Binding();
bind.Path = new PropertyPath("Selected");
chkElement.SetBinding(CheckBox.IsCheckedProperty, bind);
return dt;
}
private DataTemplate GetTextBlockTemplate(int Index)
{
TextConverter textConverter = new TextConverter();
ColorConverter colorConverter = new ColorConverter();
DataTemplate dt = new DataTemplate(typeof(TextBlock));
FrameworkElementFactory txtElement = new FrameworkElementFactory(typeof(TextBlock));
dt.VisualTree = txtElement;
Binding bind = new Binding();
bind.Path = new PropertyPath("Servo" + Index);
bind.Converter = textConverter;
txtElement.SetBinding(TextBlock.TextProperty, bind);
Binding bind1 = new Binding();
bind1.Path = new PropertyPath("Servo" + Index);
bind1.Converter = colorConverter;
txtElement.SetBinding(TextBlock.BackgroundProperty, bind1);
return dt;
}
}
输出值
笔记
这是一个非常基本的示例,它使用所有模拟数据来模拟您在问题中提到的场景。我上了每节课,转换器只在单页上。在管理项目的适当文件夹结构时,您需要照顾对象引用/名称空间。我测试了此代码,它正在作为附件图像工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句