gpt4 book ai didi

c# - 在 c# wpf 中,我可以将 listview 列动态构建为类方法吗?

转载 作者:太空宇宙 更新时间:2023-11-03 12:14:46 25 4
gpt4 key购买 nike

我将使用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]} ......

(伺服[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 和 Helm 机的值返回颜色和显示。

我已经多次重复 GridViewColumn block ,这看起来真的很虚,并且已经创建了 40 个虚属性,因为它不能直接绑定(bind)到方法。

请问有没有简单的方法来构建 ListView ?

其实数组的大小并不是固定的,21只是最大的大小。

我想知道它是否可以动态构建列,以便可以根据实际大小创建列,并且可以在循环中完成那些重复的列。并根据类方法设置背景和数据内容,这样我就可以直接设置为 GetLED(?) & GetServo(?) 而无需为其构建虚拟属性。

提前致谢。

最佳答案

您的问题有点冗长且合乎逻辑。我试图实现您需要的功能。

请抛出以下内容:

模型

我将 LED 和 Servo detail 划分到不同的类,并将该类的 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;
}

}

输出

enter image description here

注意事项

这是一个非常基本的示例,它使用所有模拟数据模拟您在问题中提到的场景。我参加了所有类(class),仅在单页中进行了转换器。在为项目管理正确的文件夹结构时,您需要注意对象引用/命名空间。我测试了这段代码,它作为附件工作。

关于c# - 在 c# wpf 中,我可以将 listview 列动态构建为类方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50322543/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com