ControlTemplate(Style) binding ItemsSource property

时间:2015-06-26 09:46:41

标签: xaml data-binding winrt-xaml

I want to do a data binding in a Style(ControlTemplate) in a ListView

My ListView looks like:

<ListView ItemTemplateSelector="{StaticResource ItemTemplate_Selector}" ItemContainerStyleSelector="{StaticResource ItemContainerStyle_Selector}"/>

Most of properties fetched from ItemsSource are binding in the DataTemplate:

e.g.

<DataTemplate x:Key="OneOfItem">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Text="{Binding Name}"/>
        <StackPanel Grid.Row="1">
            <RichTextBlock Grid.Column="0" local:HyperlinkExtensions.Text="{Binding Message}"/>
            <TextBlock Grid.Column="1" Text="{Binding Time}"/>
        </StackPanel>
    </Grid>
</DataTemplate>

For the reason that there's a area that will be affected by different visual state and there's a property displayed in that area, so I have to binding it in the ItemContainerStyle like:

e.g.

<Style x:Key="HeadPicStyle" TargetType="ListViewItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListViewItem">                    
                <Border>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition x:Name="Header" Width="20"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid Grid.Column="0">
                            <TextBlock x:Name="HeaderText" Text="{Binding Header}"/>
                        </Grid>
                        <ContentPresenter Grid.Column="1" x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                    <VisualStateManager.VisualStateGroups>                            
                        <VisualStateGroup x:Name="AdaptiveStatesTest">
                            <VisualState x:Name="DefaultStateTest">
                                <VisualState.StateTriggers>
                                    <AdaptiveTrigger MinWindowWidth="{StaticResource WideStateChangeThreshold}" />
                                </VisualState.StateTriggers>
                            </VisualState>
                            <VisualState x:Name="NarrowStateTest">
                                <VisualState.StateTriggers>
                                <AdaptiveTrigger MinWindowWidth="{StaticResource NarrowStateWindowWidth}" />
                                </VisualState.StateTriggers>
                                <VisualState.Setters>
                                    <Setter Target="Header.Width" Value="100"/>
                                </VisualState.Setters>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>                        
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

On my perspective, the itemContainerStyle is targeted to the itemSource in the ListView, so I can access it's property (means I can directly binding the property name)

However, the HeaderText's text can not be seen in the app.

I'm wondering if my binding in the style is wrong or not?

0 个答案:

没有答案