简体   繁体   English

在WPF中,当EventTrigger Source在页面资源之外但Storyboard.TargetName在树视图数据模板中时,该怎么办?

[英]In WPF, how to do when EventTrigger Source is outside a page's resources but Storyboard.TargetName is in a treeview datatemplate?

I have a TreeView whose <ItemTemplate> is defined in HierarchicalDataTemplate 我有一个TreeView<ItemTemplate>在HierarchicalDataTemplate中定义

 <HierarchicalDataTemplate ItemsSource="{Binding}"
                              x:Key="TreeTemplate">
    <StackPanel Orientation="Horizontal">
        <TextBlock Name="TextBlockProperty" Text="{Binding Name}" Width="110" Foreground="#FF3C3C3C" >
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ModifyType}" Value="AutoDetect">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <SolidColorBrush Color="LightGreen"></SolidColorBrush>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ModifyType}" Value="Prerequisite">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <SolidColorBrush Color="LightSkyBlue"></SolidColorBrush>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ModifyType}" Value="AutoCheckFailed">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <SolidColorBrush Color="Red"></SolidColorBrush>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
        <ContentControl Name="ContentCtrl" Content="{Binding}">
        </ContentControl>
    </StackPanel>
    <HierarchicalDataTemplate.Triggers>
        <DataTrigger Binding="{Binding ControlType}" Value="Text">
            <Setter TargetName="ContentCtrl" Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBox Width="130" Text="{Binding Value}" BorderThickness="0" Background="#FFF8F8F8"></TextBox>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding ControlType}" Value="Choice">
            <Setter TargetName="ContentCtrl" Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <ComboBox Width="130" IsEditable="True" ItemsSource="{Binding ChoiceItems}" Text="{Binding Value}" BorderThickness="0" >
                        </ComboBox>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding ControlType}" Value="Group">
            <Setter TargetName="ContentCtrl" Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <TextBlock></TextBlock>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>

I have also defined 2 <Buttons> ShowDefault and HideDefault outside the <Page.Resources> to shorten/widen the width of the TextBlockProperty <TextBlock> in the <HierarchicalDataTemplate> by using Storyboard . 我还定义2 <Buttons>ShowDefaultHideDefault<Page.Resources> 缩短/加宽 TextBlockProperty 的宽度 <TextBlock><HierarchicalDataTemplate>通过使用Storyboard But I don't know how to do it. 但是我不知道该怎么做。

Because it is a TreeView , the Items are actually several columns and rows, more like a form, binded in the <HierarchicalDataTemplate> . 因为它是TreeView ,所以Items实际上是绑定在<HierarchicalDataTemplate>中的几行和几行,更像是一种表单。 But the 2 Buttons are defined in a Grid outside the <Page.Resources> ...In order to do the Width shortening/widening , I use Storyboard in <Page.Resources> . 但是2个按钮是在<Page.Resources>之外的Grid中定义的...为了进行宽度的缩短/加宽 ,我在<Page.Resources>使用Storyboard

<Storyboard x:Key="ShowDefault">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Width" Storyboard.TargetName="GroupList">
            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Width" Storyboard.TargetName="PropertyTree">
            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="270"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Width" Storyboard.TargetName="DefaultTree">
            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="182"/>
        </DoubleAnimationUsingKeyFrames>

    </Storyboard>
    <Storyboard x:Key="HideDefault">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Width" Storyboard.TargetName="GroupList">
            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="152"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Width" Storyboard.TargetName="PropertyTree">
            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="299"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Width" Storyboard.TargetName="DefaultTree">
            <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>

And call the Storyboard like this: 然后像这样调用Storyboard

<Page.Triggers>
    <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="ShowDefaultValue">
        <BeginStoryboard Storyboard="{StaticResource ShowDefault}"/>
    </EventTrigger>
    <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="HideDefaultValue">
        <BeginStoryboard Storyboard="{StaticResource HideDefault}"/>
    </EventTrigger>
</Page.Triggers>

A little complicated, but hope you get it right. 有点复杂,但是希望您做对了。

Any help or suggestions will be appreciated. 任何帮助或建议,将不胜感激。 :) :)

I'm not sure what are you trying to achieve, so I must guess what you meant :). 我不确定您要达到什么目标,所以我必须猜出您的意思是:)。

To get the reference to the TextBlock you can attached to its Loaded event like this: 要获取对TextBlock的引用,可以将其附加到Loaded事件,如下所示:

<TextBlock Loaded="TextBoxLoaded"></TextBlock>
 private TextBlock _textBlock;
        private void TextBoxLoaded(object sender, RoutedEventArgs e)
        {
            _textBlock = sender as TextBlock;
        }

and then when the button is clicked, changed the properties of the TextBlock. 然后单击按钮时,更改TextBlock的属性。

Normally, the button should be in the template as well for each item of your child template. 通常,对于子模板的每个项目,按钮也应位于模板中。

OK, I eventually solve the problem by adding a <ScrollView> outside the <StackPanel> of two <TreeView> s. 好的,我最终通过在两个<TreeView><StackPanel>之外添加一个<ScrollView>解决该问题。 No need to change the DataBinding or anything else. 无需更改DataBinding或其他任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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