c# - 使用转换器更改用户控件的背景颜色

标签 c# wpf user-controls

我有一个用户控件,我想动态更改此控件的背景颜色。

设置 View 模型中的某个枚举时,我希望颜色发生变化,因此我使用转换器将枚举转换为 SolidColorBrush。

当我将这个转换器放在另一个控件中时,它工作正常。但是当我将它放在 xaml 文件顶部的 usercontrol 属性中时,它会出错。

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:ee="http://schemas.microsoft.com/expression/2010/effects"
mc:Ignorable="d"
xmlns:converters="clr-namespace:UserControlSolution.Converter"
x:Class="UserControlSolution.UserControlButton"
x:Name="UserControl"
Height="50" 
VerticalAlignment="Top"
Background="{Binding CallStatus, Converter={StaticResource CallStatusBackgroundConverter}}"
Margin="2,0,0,5"
>

<UserControl.Resources>
    <converters:CallStatusEnumToBackgroundColor x:Key="CallStatusBackgroundConverter"/>
    <converters:StatusEnumToStatusResourceConverter x:Key="StatusIconConverter"/>
    <BooleanToVisibilityConverter x:Key="BoolToVis" />

    <Style x:Key="SelectedStyle" TargetType="{x:Type WrapPanel}">
        <Setter Property="Background" Value="Transparent"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="true">
                <Setter Property="Background" Value="{Binding CallStatus, Converter={StaticResource CallStatusBackgroundConverter}}"/>
                <Setter Property="Opacity" Value=".92"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" VerticalAlignment="Top" HorizontalAlignment="Stretch">        
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="30"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="50"/>
    </Grid.ColumnDefinitions>
 </Grid>

我的转换器只是从枚举转换为颜色

namespace UserControlSolution.Converter
{
    [ValueConversion(typeof(CallEnum), typeof(SolidColorBrush))]
    public class CallStatusEnumToBackgroundColor : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            switch ((CallEnum)value)
            {
                case CallEnum.Connected:
                    return (SolidColorBrush)Application.Current.Resources["Red"];
                case CallEnum.ConnectedIntern:
                    return (SolidColorBrush)Application.Current.Resources["Blue"];
                case CallEnum.Hold:
                    return (SolidColorBrush)Application.Current.Resources["Orange"];
                case CallEnum.Idle:
                    return (SolidColorBrush)Application.Current.Resources["DarkGrey"];
                case CallEnum.OffRing:
                    return (SolidColorBrush)Application.Current.Resources["Yellow"];
                default:
                    return null;
            }
        }

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

最佳答案

将其放在资源之后并从用户控件中删除绑定(bind):

<UserControl.Background>
   <SolidColorBrush Color="{Binding CallStatus, Converter={StaticResource CallStatusBackgroundConverter}}"/>
</UserControl.Background>

关于c# - 使用转换器更改用户控件的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18781886/

相关文章:

c# - 如何在 C# WinForm 自定义用户控件中设置文本对齐指示器?

c# - 为什么跨线程以这种方式工作?

c# - 在不同窗口中单击按钮时调用函数(WPF 应用程序)

wpf - 为什么我的 GUI 卡住?

c# - 我可以在 Mouse.Capture() 处于事件状态时获得 MouseLeave 事件吗?

C# 用户控件 : access controls properties

c# - 基于用户权限的 MEF 组件

c# - 我如何通过二维数组 'foreach'?

c# - 如何测试按位枚举是否包含 C# 中另一个按位枚举的任何值?

c# - 类型化 HttpClient 与 IHttpClientFactory