【问题标题】:In WPF, is it possible for the XAML to obtain the color of the currently rendered background?在 WPF 中,XAML 是否可以获取当前渲染背景的颜色?
【发布时间】:2015-07-03 10:47:15
【问题描述】:

我有一个显示进度条的控件。

我希望进度条的颜色有效地成为当前渲染背景颜色的更强版本,例如:

  • 如果背景是浅黄色,我希望进度条是深黄色。
  • 如果背景是浅绿色,我希望进度条是深绿色。

这在 WPF 中可行吗?

请注意,我不知道是谁在为我设置背景颜色,所以我无法真正手动设置。

更新

我应该澄清一些父控件在 XAML 中将Background 设置为transparent

然而,就可视化树而言,这仅仅意味着Background 被传递给所有的孩子。

【问题讨论】:

  • 不能只绑定当前控件模板背景吗?
  • 添加一个通过 VisualTree 获取父级背景的附加属性怎么样?
  • 尝试使用父窗口的背景((Window)this.Parent).Background
  • 您是否尝试实现主题?使用这种方法(具有不同的样式/画笔/等),您不会在视图或 ViewModel(mvvm?)中执行任何操作,但它是通过使用相同的样式键自动完成的,但合并了不同的 ResourceDisctionary。跨度>
  • @Simplyvaibh 感谢您的提示,但我使用的是 MVVM,因此避免使用后面的代码。

标签: c# .net wpf xaml mvvm


【解决方案1】:

如果您知道哪种类型的控件使用 RelativeSource 将背景绑定到它。然后根据检索到的 Brush 将其调整为您的需求。

使用 Converter 的第一种方法如下:

 class BackgroundConverter : IValueConverter
 {
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
     {
         var background = value as SolidColorBrush;
         if (background.Color == Colors.LightYellow) return new SolidColorBrush(Colors.Yellow);
         if (background.Color == Colors.LightGreen) return new SolidColorBrush(Colors.Green);
         return new SolidColorBrush(Colors.White);
     }

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
     {
         throw new NotImplementedException();
     }
}

XAML:

<Window.Resources>
    <local:BackgroundConverter x:Key="BackgroundConverter"/>
</Window.Resources>

<ProgressBar Background="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Background,
      Converter={StaticResource BackgroundConverter}}"/>

或仅使用 XAML 的后一种方法:

<ProgressBar>
    <ProgressBar.Style>
        <Style TargetType="ProgressBar">
            <Setter Property="Background" Value="White"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Background}" Value="LightYellow">
                    <Setter Property="Background" Value="Yellow"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Background}" Value="LightGreen">
                    <Setter Property="Background" Value="Green"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ProgressBar.Style>
</ProgressBar>

【讨论】:

  • 这还不是全部。使用此绑定,ProgreeBar 的背景将具有与背景相同的颜色。我认为他需要提供一个转换器 woo,在那里他根据渲染的背景颜色调整颜色
  • 感谢您的精彩回答,这非常有效。
【解决方案2】:

您可以使用如下常用样式的进度条,并在相应的ViewModel中绑定前景色。 vm:ProgressBar 是一个用户控件

 <vm:ProgressBar x:Name="ProgressBar"
                                        Grid.RowSpan="3"
                                        Width="140"
                                        Height="120"
                                        Margin="12"
                                        Panel.ZIndex="5"
                                        Padding="10"
                                       DataContext.ProgressBarVisibility,
                                                             RelativeSource={RelativeSource AncestorType=Window,
                                                                                            AncestorLevel=1}}"
                                        d:DesignerVisibility="False">
                            <vm:ProgressBar.Foreground>
                                <RadialGradientBrush Center="0.5,0.5" GradientOrigin="0.4,0.4" RadiusX="0.5" RadiusY="0.5">
                                    <RadialGradientBrush.GradientStops>
                                        <GradientStop Offset="0" Color="Transparent" />
                                        <GradientStop Offset="1" Color="{Binding Path=FColor}" />
                                    </RadialGradientBrush.GradientStops>
                                </RadialGradientBrush>
                            </vm:ProgressBar.Foreground>
                        </vm:ProgressBar>

【讨论】:

  • 感谢您的出色回答。我特别喜欢知道可以使用d:DesignerVisibility="False"
猜你喜欢
  • 2020-04-21
  • 2010-12-30
  • 1970-01-01
  • 2019-09-01
  • 2017-10-16
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多