在 WPF 中的数据网格列中写入状态

我有一个简单的 WPF 应用程序,它使用从绑定列表收集的数据填充行,它看起来像这样:

http://img1.mukewang.com/61c6f2130001bb5407990448.jpg

现在你可以看到我的第一列是空的,但我想写下取决于sts列的信息。例如:如果sts == 8我想在第一个空白列中写下字母 R 并在sts == 0. 我怎样才能做到这一点?这是我的 WPF 的回溯代码:


<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; 

}


慕的地6264312
浏览 243回答 2
2回答

繁星淼淼

你可以更换空DataGridTextColumn了DataGridTemplateColumn,有一个CellTemplate用DataTrigger:<DataGridTemplateColumn>&nbsp; &nbsp; <DataGridTemplateColumn.CellTemplate>&nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBlock>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBlock.Style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Style TargetType="TextBlock">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Style.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTrigger Binding="{Binding cflag}" Value="8">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="Text" Value="R" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTrigger>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Style.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </TextBlock.Style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </TextBlock>&nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; </DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn>或者您可以向您的PIO_CARD类添加另一个属性并绑定到这个属性:public string FirstValue{&nbsp; &nbsp; get { return cflag == "8" ? "R" : string.Empty; }}&nbsp;XAML:<DataGridTextColumn Binding="{Binding FirstValue}"/>

噜噜哒

您可以编写一个DataTrigger或您可以扩展您的PIO_CARD类并添加一个属性,您可以将第一列绑定到:public class ExtendedPIO_CARD : PIO_CARD{&nbsp; &nbsp; public string FirstColumn&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (cflag == 8)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "R";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(cflag == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; string.Empty;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "default";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP