元素不采用 FallbackValues(对于 Style 属性) [英] Elements don't take FallbackValues (for the Style properties)

查看:27
本文介绍了元素不采用 FallbackValues(对于 Style 属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发 UserControl 时遇到问题.

I'm facing a problem with a UserControl development.

UserControl 公开了几个 DP(样式类型).UserControl 的元素将它们的 Style 属性绑定到它们.为了在用户未定义样式时显示某些内容,UserControl 为这些绑定提供 FallbackValues(在 UserControl.Resources 部分中定义的样式).问题是 UserControl 的唯一一个元素采用 FallbackValue,其他元素 - 不是.似乎有些东西只是覆盖了 FallbackValues.此外,VS 设计器正常显示 UserControl 本身(我的意思是,似乎 FallbackValues 被正确采用).但是当 UserControl 驻留在另一个 UserControl 或窗口中时,我会看到之前描述的行为.

The UserControl exposes a couple of DPs (of the Style type). The UserControl's elements bind their's Style properties to them. In order to show something if Styles are not defined by the user, the UserControl provide FallbackValues (Styles defined in the UserControl.Resources section) for those bindings. The issue is that the only one of the UserControl's elements take the FallbackValue, the others - not. It seems like something just overrides FallbackValues. Moreover, the VS designer show the UserControl itself normally (I mean, it seems that FallbackValues are taken properly). But when the UserControl resides in the other UserControl or a Window I see the behaviour descibed previously.

这是UserControl的xaml:

Here is the xaml of the UserControl:

<UserControl
Background="White" Style="{Binding UserControlStyle}"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<UserControl.Resources>      
    <LinearGradientBrush x:Key="GeneralKeyBackground" StartPoint="0.5,0" EndPoint="0.5,1">
        <GradientStop Offset="0" Color="#FFA0A9B2" />
        <GradientStop Offset="1" Color="#FF5B626A" />            
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="StationNameBackground" StartPoint="0.5,0" EndPoint="0.5,1">
        <GradientStop Offset="0" Color="#FFC4E3FF" />
        <GradientStop Offset="0.462" Color="#FFE7F3FF" />
    </LinearGradientBrush>

    <Style x:Key="DefaultStationNameForSearchingStyle" TargetType="TextBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Grid>
                        <TextBox TextAlignment="Center" Text="{TemplateBinding Text}" 
                                 Background="Transparent" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="FontSize" Value="22" />
        <Setter Property="Foreground" Value="Black" />
    </Style>

    <Style x:Key="DefaultButtonsStyle" TargetType="customControls:RoundButton">
        <Setter Property="Template" Value="{DynamicResource RoundButtonTemplate}" />
        <Setter Property="Width" Value="85" />
        <Setter Property="Height" Value="75" />          
        <Setter Property="FontFamily" Value="Verdana" />
        <Setter Property="FontSize" Value="22" />
        <Setter Property="Background" Value="{StaticResource GeneralKeyBackground}" />
        <Setter Property="Foreground" Value="White" />
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground" Value="Gray" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <DataTemplate x:Key="StationTemplate">
        <customControls:RoundButton MinWidth="350" MinHeight="60" DataContext="{Binding}" Content="{Binding }" />
    </DataTemplate>

    <Style x:Key="DefaultTitleStyle" TargetType="TextBlock">
        <Setter Property="Control.FontSize" Value="48" />        
        <Setter Property="Control.Foreground" Value="Black" />
    </Style>       

    <Style x:Key="DefaultStationNameForSearchingBorderStyle" TargetType="Border">           
        <Setter Property="BorderBrush" Value="#FF2C8BF5" />
        <Setter Property="Background" Value="{StaticResource StationNameBackground}" />
    </Style>
    <!-- EndRegion -->

</UserControl.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <TextBlock Text="Select Station"
               Style="{Binding TitleStyle, FallbackValue={StaticResource DefaultTitleStyle}}" />

    <userControls:ScrollableWrapPanel x:Name="StationsPanel" Grid.Row="1"  ItemsSource="{Binding FilteredStations}"
                                      ItemTemplate="{StaticResource StationTemplate}"
                                      ButtonsStyle="{StaticResource DefaultButtonsStyle}" />

    <Border Grid.Row="2" MinWidth="460" MinHeight="65"
            Style="{Binding StationNameForSearchingBorderStyle, FallbackValue={StaticResource DefaultStationNameForSearchingBorderStyle}}">
        <TextBox x:Name="StationName" Width="420" Height="55" SelectionChanged="TextBoxBase_OnSelectionChanged"
                 Style="{Binding StationNameForSearchingStyle, FallbackValue={StaticResource DefaultStationNameForSearchingStyle}}" />
    </Border>

    <Border Grid.Row="3" Style="{Binding KeyboardBorderStyle}">
        <virtualKeyboard:VirtualKeyboard x:Name="VirtualKeyboard" 
                         GeneralKeyStyle="{Binding KeyboardButtonsStyle, FallbackValue={StaticResource DefaultButtonsStyle}}"/>
    </Border>
</Grid>

VirtualKeyboard 按钮始终采用 FallbackValue,它可以正常工作.其他的不是.你有什么建议来调试这种非常奇怪的行为?

VirtualKeyboard buttons take the FallbackValue always, it just works. The others are not. What can you advice to debug this very strange behaviour?

推荐答案

BindingBase.FallbackValue 状态

绑定成功返回一个值,如果:

  1. 绑定源的路径解析成功.
  2. 值转换器(如果有)能够转换结果值.
  3. 结果值对绑定目标(target)属性有效.

如果 1 和 2 返回 DependencyProperty.UnsetValue,则目标属性设置为 FallbackValue 的值(如果有).

因此,如果您的任何 DependencyProperties 被设置或返回 [null] 值,它将被视为有效绑定并且不会使用 FallbackValue.

So, if any of your DependencyProperties are being set or returning a [null] value, it will be considered a valid bind and the FallbackValue will not be used.

我不知道这是否是您的问题,因为我需要查看您的更多解决方案,但值得一试.

I do not know if this is your problem as I would need to see more of your solution, but it would be worth checking into.

我希望这会有所帮助.

这篇关于元素不采用 FallbackValues(对于 Style 属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆