简体   繁体   中英

Add text on image dynamically in WPF with C#

I am trying to achieve some thing below in the image. In the list, I would like to put the text "WS1", "WS2" "WS3" onto the image dynamically in the code depending on the some conditions. I have setup something in xaml. I am not sure if that will work and how to write the code to match the 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>

How do I implement this in the code and does this xaml work? thank you very much in advance.

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM