动态添加矩形到WPF ListView [英] Add dynamically a rectangle to a WPF ListView
本文介绍了动态添加矩形到WPF ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理一个项目,我正在制作一个带有颜色和一些文字的列表。
添加文字和颜色代码它工作正常。
这是XAML代码:
Hi,
I am working with a project there I am making a list with colours and some text.
To add text and colour codes it's working fine.
Here is the XAML code:
<ListView Grid.Row="0" Margin="5" Name="lvCustomStream" Width="Auto">
<ListView.View>
<GridView >
<GridViewColumn Header="Length" Width="90" DisplayMemberBinding="{Binding Length}" />
<GridViewColumn Header="Speed" Width="90" DisplayMemberBinding="{Binding Speed}" />
<GridViewColumn Header="Colour1" Width="90" DisplayMemberBinding="{Binding Colour1}" />
<GridViewColumn Header="Colour2" Width="90" DisplayMemberBinding="{Binding Colour2}" />
</GridView>
</ListView.View>
</ListView>
<Grid Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="2">
<Button x:Name="btCsSave" Width="40" Margin=" 5" Content="Save" Click="btCsSave_Click"/>
<Button x:Name="btnCsCancel" Width="40" Margin="5" Content="Cancel" Click="btnCsCancel_Click"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="1">
<Label Margin="5,5,0,5" Width="Auto" Content="Length"/>
<TextBox x:Name="txtCsLength" Margin="0,5,5,5" Width="40"/>
<Label Margin="5,5,0,5" Width="Auto" Content="Speed"/>
<TextBox x:Name="txtCsSpeed" Margin="0,5,5,5" Grid.Row="1" Width="40"/>
<Label Margin="5,5,0,5" Width="Auto" Content="Colour1"/>
<Rectangle x:Name="rectCsColour1" Margin="0,5,5,5" Grid.Row="1" Width="25" MouseDown="txtCsColour1_MouseDown" Fill="#FFFBD904"/>
<Label Margin="5,5,0,5" Width="Auto" Content="Colour2"/>
<Rectangle x:Name="rectCsColour2" Margin="0,5,5,5" Grid.Row="1" Width="25" Fill="#FFF9F904" MouseDown="rectCsColour2_MouseDown"/>
</StackPanel>
</Grid>
背后的代码:
The code behind:
private void btCsSave_Click(object sender, RoutedEventArgs e)
{
if (txtCsThreadLength.Text == "") return;
lvCustomStream.Items.Add(new CsItem(txtCsLength.Text.ToString(), txtCsSpeed.Text.ToString(), rectCsColour1.Fill.ToString(), rectCsColour1, rectCsColour2.Fill.ToString()));
}
缺少的是:当我点击保存按钮时,我想让两个矩形进入两个颜色值之后的ListView,而不仅仅是两个颜色值,速度和长度。
提前谢谢
Vidor
What is missing is: when I click on the save button, I want to get the two rectangles too into the ListView after the two colour values, not just the two Colour-values, Speed and Length.
Thanks in advance
Vidor
推荐答案
GridViewColumn上的DisplayMemberBinding将显示绑定属性的文本表示。
您需要设置GridViewColumn的CellTemplate到你想要显示数据的模板,
DisplayMemberBinding on GridViewColumn will display thetext representation of the binding property.
You need to set CellTemplate of GridViewColumn to a template that you want to display the data, along the lines
<gridviewcolumn header="Colour1" width="90">
<gridviewcolumn.celltemplate>
<datatemplate>
<rectangle fill="{Binding}"></rectangle>
</datatemplate>
</gridviewcolumn.celltemplate>
</gridviewcolumn>
这篇关于动态添加矩形到WPF ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文