使用C#

时间:2018-10-26 01:55:06

标签: c# wpf image xaml testing

我正在尝试实现图片中的以下内容。 在列表中,我想根据某些条件在代码中将文本“ WS1”,“ WS2”,“ WS3”动态地放在图像上。我已经在xaml中设置了一些东西。我不确定这是否行得通,以及如何编写代码以匹配xaml。

       <Style x:Key="AnnotationStyle" TargetType="TextBlock">
        <Setter Property="Background" Value="#70FFFFFF" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="TextAlignment" Value="Center"/>
        <Setter Property="TextWrapping" Value="Wrap"/>
    </Style>
    <DataTemplate x:Key="DisplayImage">
        <StackPanel Width="50">
            <StackPanel Orientation="Horizontal">
                <Image Height="40" Source="{Binding ImageData}" />
                <TextBlock Text="{Binding Description}" Style="{StaticResource AnnotationStyle}"/>
            </StackPanel>
        </StackPanel>
    </DataTemplate>

 <ListView.View>
     <GridView>
         <GridViewColumn CellTemplate="{StaticResource checkbox}"/>
         <GridViewColumn Header="Profile ID" DisplayMemberBinding="{Binding ProfileID}"/>
                <GridViewColumn Header="Monitor 1" DisplayMemberBinding="{Binding DisplayImage}">
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

我如何在代码中实现此功能,此xaml是否有效?提前非常感谢您。

enter image description here

1 个答案:

答案 0 :(得分:0)

 <ListView>
        <ListView.View>
            <GridView>
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <CheckBox />
                            </Grid>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn
                    Width="50"
                    DisplayMemberBinding="{Binding ProfileID}"
                    Header="Profile ID" />
                <GridViewColumn Width="90" Header="Monitor 1">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <Image
                                    Width="50"
                                    Height="40"
                                    Source="{Binding DisplayImage}" />
                                <TextBlock
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    Text="{Binding Text1}" />
                            </Grid>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Width="90" Header="Monitor 2">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <Image
                                    Width="50"
                                    Height="40"
                                    Source="{Binding DisplayImage}" />
                                <TextBlock
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    Text="{Binding Text2}" />
                            </Grid>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Width="90" Header="Monitor 3">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <Image
                                    Width="50"
                                    Height="40"
                                    Source="{Binding DisplayImage}" />
                                <TextBlock
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    Text="{Binding Text3}" />
                            </Grid>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>