none
请教:关于Expander的Header绑定的问题 RRS feed

  • 问题

  • 我希望在Expender的Header上绑定一个DataTemplate,但是现在显示的是空行,请问如何修改?代码如下,红色加粗的地方是我现在的写法

     

    <UserControl x:Class="ChangeRequestSystem.CustomControl.UpdateHistoryList"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="299" Width="350">
        <UserControl.Resources>
            <XmlDataProvider x:Key="myData" XPath="Items/Item">
                <x:XData>
                    <Items xmlns="">
                        <Item>
                            <Updator>Bill Tang</Updator>
                            <Date>6/14/2007 11:27:00 AM</Date>
                            <Description>It is because of configuration of Web.Config about Reporting Service</Description>
                        </Item>
                    </Items>
                </x:XData>
            </XmlDataProvider>
            <DataTemplate x:Key="updatorAndDate">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding XPath = Updator}"></TextBlock>
                    <TextBlock Text="    "></TextBlock>
                    <TextBlock Text="{Binding XPath = Date}"></TextBlock>
                </StackPanel>
            </DataTemplate>
            <DataTemplate x:Key="updateItem">
                <Expander Name="exp" IsExpanded="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}},Path=IsSelected}" HeaderTemplate="{StaticResource updatorAndDate}" Width="300">
                    <TextBlock FontSize="12" TextWrapping="Wrap" Text="{Binding XPath=Description}"/>
                </Expander>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}, AncestorLevel=1}, Path=IsSelected}" Value="True">
                        <Setter Property="IsExpanded" Value="True" TargetName="exp" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </UserControl.Resources>
        <Grid>
            <ListBox IsSynchronizedWithCurrentItem="True"
            ItemsSource="{Binding Source={StaticResource myData}}"
            ItemTemplate="{StaticResource updateItem}" />
        </Grid>
    </UserControl>
    2008年3月10日 1:29

答案

  • 2008-3-10  在Expender的Header上绑定一个DataTemplate的方法:

    <UserControl x:Class="ChangeRequestSystem.CustomControl.UpdateHistoryList"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="299" Width="350">
        <UserControl.Resources>
            <XmlDataProvider x:Key="myData" XPath="Items/Item">
                <x:XData>
                    <Items xmlns="">
                        <Item>
                            <Updator>Bill Tang</Updator>
                            <Date>6/14/2007 11:27:00 AM</Date>
                            <Description>It is because of configuration of Web.Config about Reporting Service</Description>
                        </Item>
                    </Items>
                </x:XData>
            </XmlDataProvider>
            <DataTemplate x:Key="updatorAndDate">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding XPath = Updator}"></TextBlock>
                    <TextBlock Text="    "></TextBlock>
                    <TextBlock Text="{Binding XPath = Date}"></TextBlock>
                </StackPanel>
            </DataTemplate>
            <DataTemplate x:Key="updateItem">
                <Expander Name="exp" IsExpanded="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}},Path=IsSelected}"  Header="{Binding}" HeaderTemplate="{StaticResource updatorAndDate}" Width="300">
                    <TextBlock FontSize="12" TextWrapping="Wrap" Text="{Binding XPath=Description}"/>
                </Expander>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}, AncestorLevel=1}, Path=IsSelected}" Value="True">
                        <Setter Property="IsExpanded" Value="True" TargetName="exp" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </UserControl.Resources>
        <Grid>
            <ListBox IsSynchronizedWithCurrentItem="True"
            ItemsSource="{Binding Source={StaticResource myData}}"
            ItemTemplate="{StaticResource updateItem}" />
        </Grid>
    </UserControl>
     
    2008年3月21日 8:49