开发者

How to set property only on second column of a ListView?

Introduction

I have a ListView and want to format only the second column. The following XAML code does that:

<ListView x:Name="listview">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Property" DisplayMemberBinding="{Binding Path=Key}" Width="100"/>
            <!-- <GridViewColumn Header="Value" DisplayMemberBinding="{Binding Path=Value}" Width="250">-->
            <GridViewColumn Header="Value" Width="250">
                <GridViewColumn.CellTemplate>
            开发者_如何学Go        <DataTemplate>
                        <TextBlock Text="{Binding Path=Value}" Foreground="CornflowerBlue" AutomationProperties.Name="{Binding Path=Key}"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

The one problem I have is that the AutomationProperties.Name property is not being set. I was checking it with the Coded UI Test Builder and the property is empty. The Text and the Foreground property are being set correctly.

Question

Does anyone know why AutomationProperties.Name is not being set?

Additional information

Strangly enough, the following XAML code does set the AutomationProperties.Name

<ListView x:Name="listview">
   <ListView.Resources>
       <Style TargetType="TextBlock">
           <Setter Property="AutomationProperties.Name" Value="{Binding Key}"/>
        </Style>
    </ListView.Resources>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Property" DisplayMemberBinding="{Binding Path=Key}" Width="100"/>
            <GridViewColumn Header="Value" DisplayMemberBinding="{Binding Path=Value}" Width="250"/>
        </GridView>
    </ListView.View>
</ListView>

The problem here though is that AutomationProperties.Name is being set on all the columns. But I only want it on the second one because otherwise my Coded UI Test code returns the wrong value (that of the first column, instead of that of the second column which I want).


Don't know if you're aware of this, but a very helpful tool in analyzing these types of problems is Snoop.

In particular, it will highlight (with red) any data binding errors you may have.

I took a look myself and it sure seems as if the first piece of xaml (above) is now working (after you cleared up the syntax error). In Snoop, bound properties are highlighted with a light green.

Here is a screen shot of Snoop showing the property is being set correctly:

How to set property only on second column of a ListView?

And here is a screen shot of Snoop showing the TextBlock (where the property isn't set ... no light green column) on the first column:

How to set property only on second column of a ListView?

And, finally, I've intentionally broken the binding to show you what Snoop shows when something is wrong and you have a data binding error (it is highlighted in red and one of the columns gives you additional information):

How to set property only on second column of a ListView?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜