如何更改运行时gridview中存在的Ellipse颜色?(How to change the Ellipse color which is present in the gridview in runtime?)
<DataGrid AutoGenerateColumns="False" Height="330" HorizontalAlignment="Left" Margin="5,70,0,0" Name="grdDeviceInfo" VerticalAlignment="Top"
CanUserAddRows="False" IsReadOnly="True" Width="368" HorizontalScrollBarVisibility="Auto">
<DataGrid.Columns>
<DataGridTextColumn Header="Channels" Width="100" Binding="{Binding ChannelNo}"></DataGridTextColumn>
<DataGridTextColumn Header="Label" Width="200" Binding="{Binding Label}"></DataGridTextColumn>
<DataGridTemplateColumn Header="Status" Width="60">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Ellipse Height="13" Name="grdfEllipse" Stroke="Black" Fill="{}" Width="20"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
代码背后:
List<IOInformation> lstIOStatus = new List<IOInformation>(); for (int i = 0; i < channelStatus.Count(); i++) { // var columns = line.Split('\t'); lstIOStatus.Add(new IOInformation { ChannelNo = "Channel " + j++, Label = labeldata[i], MasterStatus = Convert.ToBoolean(channelStatus[i]) }); } this.grdDeviceInfo.ItemsSource = lstIOStatus;我想将椭圆颜色更改为绿色(如果为真),或者红色(如果为假)。
<DataGrid AutoGenerateColumns="False" Height="330" HorizontalAlignment="Left" Margin="5,70,0,0" Name="grdDeviceInfo" VerticalAlignment="Top" CanUserAddRows="False" IsReadOnly="True" Width="368" HorizontalScrollBarVisibility="Auto"> <DataGrid.Columns> <DataGridTextColumn Header="Channels" Width="100" Binding="{Binding ChannelNo}"></DataGridTextColumn> <DataGridTextColumn Header="Label" Width="200" Binding="{Binding Label}"></DataGridTextColumn> <DataGridTemplateColumn Header="Status" Width="60"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Ellipse Height="13" Name="grdfEllipse" Stroke="Black" Fill="{}" Width="20"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>Code Behind:
List<IOInformation> lstIOStatus = new List<IOInformation>(); for (int i = 0; i < channelStatus.Count(); i++) { // var columns = line.Split('\t'); lstIOStatus.Add(new IOInformation { ChannelNo = "Channel " + j++, Label = labeldata[i], MasterStatus = Convert.ToBoolean(channelStatus[i]) }); } this.grdDeviceInfo.ItemsSource = lstIOStatus;I want to change the Ellipse color to green if it is true, or red if it is false.
最满意答案
您可以使用触发器执行此操作。 以下是关于如何使用它们的链接:
http://www.wpf-tutorial.com/styles/trigger-datatrigger-event-trigger/
您的xaml代码如下所示:
<Ellipse Height="13" Name="grdfEllipse" Stroke="Black" Width="20"> <Ellipse.Style> <Setter Property="Fill" Value="Red"/> <Style TargetType="Ellipse"> <Style.Triggers> <DataTrigger Binding="{Binding yourBinding}" Value="true"> <Setter Property="Fill" Value="Green"/> </DataTrigger> </Style.Triggers> </Style> </Ellipse.Style> </Ellipse>You can use triggers to do this. Here is a link on how top use them :
http://www.wpf-tutorial.com/styles/trigger-datatrigger-event-trigger/
Your xaml code would look like this :
<Ellipse Height="13" Name="grdfEllipse" Stroke="Black" Width="20"> <Ellipse.Style> <Setter Property="Fill" Value="Red"/> <Style TargetType="Ellipse"> <Style.Triggers> <DataTrigger Binding="{Binding yourBinding}" Value="true"> <Setter Property="Fill" Value="Green"/> </DataTrigger> </Style.Triggers> </Style> </Ellipse.Style> </Ellipse>更多推荐
lstIOStatus,Width,grdDeviceInfo,电脑培训,计算机培训,IT培训"/> <meta name=&qu
发布评论