【问题标题】:ControlTemplate DataTrigger not Updating when Bound to Another Control绑定到另一个控件时,ControlTemplate DataTrigger 不更新
【发布时间】:2020-09-07 04:24:19
【问题描述】:

我一直在创建一个自定义控件,其中包括一个位于 Datagrid 顶部的组面板。

我有一个水印文本块,一旦我通过代码隐藏将子项添加到 PART_GroupPanel 后,该文本块应该折叠。

以下代码是我将在整个应用程序中多次使用的 ControlTemplate 的一部分。

<!--Watermark Textblock-->
<TextBlock Text="Drag &amp; Drop Column to Group." Foreground="{DynamicResource FirstThemeBrush}" FontFamily="Calibri" Margin="8,0,0,0" VerticalAlignment="Center">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=PART_GroupPanel, Path=Children.Count}" Value="0">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>
<!--Panel-->
<StackPanel x:Name="PART_GroupPanel" Margin="0,1,0,2" Orientation="Horizontal">
    <StackPanel.Resources>
        <Style TargetType="Button" BasedOn="{StaticResource RoundedButtonStyle}">
            <Setter Property="Padding" Value="15,5"/>
            <Setter Property="Margin" Value="0,0,0,0"/>
        </Style>
        <Style TargetType="Path">
            <Setter Property="Width" Value="5"/>
            <Setter Property="Height" Value="8"/>
            <Setter Property="Margin" Value="5,0"/>
            <Setter Property="Fill" Value="{DynamicResource FirstThemeBrush}"/>
            <Setter Property="Stretch" Value="Fill"/>
            <Setter Property="Data" Value="M 0 6 L 2 0 L 25 25 L 2 50 L 0 42 L 16 25 L 0 6 Z"/>
        </Style>
    </StackPanel.Resources>
</StackPanel>

一旦通过后面的代码添加项目,DataTrigger 不会更新和折叠文本块。

感谢任何帮助。

【问题讨论】:

  • 我认为问题出在StackPanel.Children。由于它不是DependencyProperty 也不是实现INotifyCollectionChanged,它永远不会通过上升PropertyChanged 来通知它对绑定的更改。很遗憾,我想不出一个解决方案。

标签: wpf xaml wpf-controls datatrigger


【解决方案1】:

要解决此问题,请改用ItemsControl。它的功能本质上类似于StackPanel,因为它使用StackPanel 进行布局。

<TextBlock
    Margin="8,0,0,0"
    VerticalAlignment="Center"
    FontFamily="Calibri"
    Foreground="{DynamicResource FirstThemeBrush}"
    Text="Drag &amp; Drop Column to Group.">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Visibility" Value="Collapsed" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=PART_GroupPanel, Path=Items.Count}" Value="0">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

<ItemsControl x:Name="PART_GroupPanel"
    Margin="0,1,0,2">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

【讨论】:

    猜你喜欢
    • 2011-03-04
    • 2015-04-10
    • 2011-10-09
    • 2015-10-07
    • 2014-12-31
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多