我有一个简单的WPF应用程序,它使用从绑定列表中收集的数据填充行,如下所示:
现在您可以看到我的第一列为空,但是我想写下取决于sts
列的信息。例如:如果sts == 8
我想在第一个空白栏中写下字母R,并在sts == 0
时将其保留为空白。我怎样才能做到这一点?
这是我的WPF的backcode:
<DataGrid AutoGenerateColumns="False" Background="White" Height="316" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" Width="640" SelectionMode="Single" IsReadOnly="True" ItemsSource="{Binding Oblista}" AutoGeneratingColumn="generiseColumn" >
<!---->
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick" Handler="Row_DoubleClick"/>
<EventSetter Event="MouseRightButtonDown" Handler="Row_RightCLick"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn />
<DataGridTextColumn Header="sts" Width="50" Binding="{Binding cflag}"/>
<DataGridTextColumn Header="Adresa" Width="80" Binding="{Binding Sadr_reg}"/>
<DataGridTextColumn Header="Opis" Width="200" Binding="{Binding naziv}"/>
<DataGridTextColumn Header="Tip" Width="80" Binding="{Binding Stip_reg}"/>
<DataGridTextColumn Header="Vrednost" Width="80" Binding="{Binding sVal}"/>
<DataGridTextColumn Header="Min" Width="40" Binding="{Binding lo_limit}"/>
<DataGridTextColumn Header="Max" Width="40" Binding="{Binding hi_limit}"/>
<DataGridTextColumn Header="Rw" Width="40" Binding="{Binding write}"/>
</DataGrid.Columns>
</DataGrid>
XAML.CS:
public BindingList<PIO_CARD> Oblista = new BindingList<PIO_CARD>();
public MainWindow()
{
InitializeComponent();
dataGrid1.ItemsSource = Oblista;
}
答案 0 :(得分:3)
您可以将空的DataGridTextColumn
替换为DataGridTemplateColumn
和CellTemplate
的{{1}}:
DataTrigger
或者您可以在<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding cflag}" Value="8">
<Setter Property="Text" Value="R" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
类中添加另一个属性并绑定到该属性:
PIO_CARD
XAML:
public string FirstValue
{
get { return cflag == "8" ? "R" : string.Empty; }
}
答案 1 :(得分:0)
您可以编写DataTrigger
,也可以扩展PIO_CARD
类并添加一个属性,可以在其中将第一列绑定到:
public class ExtendedPIO_CARD : PIO_CARD
{
public string FirstColumn
{
get
{
if (cflag == 8)
{
return "R";
}
if(cflag == 0)
{
return string.Empty;
}
return "default";
}
}
}