当前位置: 首页 > 知识库问答 >
问题:

将样式控件绑定到usercontrols属性

米裕
2023-03-14

我有一个这样定义的UserControl

<UserControl x:Class="Controls.wTextBox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Controls"
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         mc:Ignorable="d" 
         d:DesignHeight="20" d:DesignWidth="150" Height="20" Width="150" BorderThickness="1" BorderBrush="LightGray" x:Name="PART_UserControl" Background="White">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <TextBox Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Center" BorderBrush="Transparent" BorderThickness="0" Name="PART_TextBox">
        <TextBox.Style>
            <Style TargetType="TextBox">
                <Style.Triggers>
                    <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                        <Setter Property="Background">
                            <Setter.Value>
                                <VisualBrush AlignmentX="Left" AlignmentY="Center" Stretch="None">
                                    <VisualBrush.Visual>
                                        <Label Foreground="DarkGray" Content="????"/>
                                    </VisualBrush.Visual>
                                </VisualBrush>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="Text" Value="{x:Null}">
                        <Setter Property="Background">
                            <Setter.Value>
                                <VisualBrush AlignmentX="Left" AlignmentY="Center" Stretch="None">
                                    <VisualBrush.Visual>
                                        <Label Foreground="DarkGray" Content="{Binding HintText, ElementName=PART_UserControl}"/>
                                    </VisualBrush.Visual>
                                </VisualBrush>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsKeyboardFocused" Value="True">
                        <Setter Property="Background" Value="White" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
    <Button Grid.Column="1" Click="OnButtonClick" Width="18" BorderBrush="Transparent" BorderThickness="0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Height="18" Focusable="False">
        <Button.Content>
            <Path Stretch="Fill" Fill="#000000" Margin="2" Data="M29.898 26.5722l-4.3921 0c-0.0118,-0.635 -0.0177,-1.0172 -0.0177,-1.1583 0,-1.4229 0.2352,-2.5929 0.7056,-3.5102 0.4704,-0.9231 1.417,-1.952 2.8281,-3.1044 1.4111,-1.1465 2.2578,-1.8991 2.5282,-2.2578 0.4292,-0.5585 0.6409,-1.1818 0.6409,-1.8579 0,-0.9408 -0.3763,-1.7463 -1.1289,-2.4224 -0.7526,-0.6703 -1.7639,-1.0054 -3.0397,-1.0054 -1.2289,0 -2.2578,0.3527 -3.0868,1.0524 -0.8232,0.6997 -1.3935,1.7698 -1.7051,3.2044l-4.4391 -0.5527c0.1234,-2.0578 0.9995,-3.8041 2.6223,-5.2387 1.6286,-1.4346 3.757,-2.152 6.4029,-2.152 2.7752,0 4.9859,0.7291 6.6322,2.1814 1.6404,1.4522 2.4635,3.1397 2.4635,5.0741 0,1.0642 -0.3057,2.0755 -0.9054,3.028 -0.6056,0.9525 -1.8933,2.2519 -3.8688,3.8923 -1.0231,0.8525 -1.6581,1.5346 -1.905,2.052 -0.2469,0.5174 -0.3587,1.4405 -0.3351,2.7752zm-4.3921 6.5087l0 -4.8389 4.8389 0 0 4.8389 -4.8389 0z"/>
        </Button.Content>
    </Button>
</Grid>
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;

namespace Controls
{
/// <summary>
/// Interaktionslogik für wTextBox.xaml
/// </summary>
public partial class wTextBox : UserControl, INotifyPropertyChanged
{
    /// <summary>
    /// Event, das ausgelöst wird, wenn sich eine Property ändert.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    #region "DependencyProperties"
    /// <summary>
    /// Bindungsproperty für den Wert, der in der TextBox angezeigt wird
    /// </summary>
    public static DependencyProperty ValueProperty;
    public static DependencyProperty DisplayTextProperty;
    public static DependencyProperty HintTextProperty;
    public static DependencyProperty HintTextColorProperty;
    public static DependencyProperty ShowHintTextProperty;
    #endregion

    /// <summary>
    /// Wert der TextBox
    /// </summary>
    public object Value
    {
        get { return (object)this.GetValue(wTextBox.ValueProperty); }
        set { this.SetValue(wTextBox.ValueProperty, value); this.OnPropertyChanged("Value"); this.OnPropertyChanged("DisplayText"); this.OnPropertyChanged("HintText"); }
    }

    public string DisplayText
    {
        get { return (Value == null) ? "" : Value.ToString(); }
        set { this.SetValue(wTextBox.ValueProperty, value); this.OnPropertyChanged("Value"); this.OnPropertyChanged("DisplayText"); this.OnPropertyChanged("HintText"); }
    }

    public string HintText
    {
        get { return (string)this.GetValue(wTextBox.HintTextProperty); }
        set { this.SetValue(wTextBox.HintTextProperty, value); this.OnPropertyChanged("HintText"); }
    }

    public Brush HintTextColor
    {
        get { return (Brush)this.GetValue(wTextBox.HintTextColorProperty); }
        set { this.SetValue(wTextBox.HintTextColorProperty, value); this.OnPropertyChanged("HintTextColor"); }
    }

    static wTextBox()
    {
        ValueProperty = DependencyProperty.RegisterAttached("Value", typeof(object), typeof(wTextBox), new PropertyMetadata(""));
        DisplayTextProperty = DependencyProperty.RegisterAttached("DisplayText", typeof(object), typeof(wTextBox), new PropertyMetadata(""));
        HintTextProperty = DependencyProperty.RegisterAttached("HintText", typeof(string), typeof(wTextBox), new PropertyMetadata(""));
        HintTextColorProperty = DependencyProperty.RegisterAttached("HintTextColor", typeof(Brush), typeof(wTextBox), new PropertyMetadata(new SolidColorBrush(Colors.LightGray)));
    }

    public wTextBox()
    {
        InitializeComponent();

        this.PART_TextBox.DataContext = this;

        this.MouseEnter += OnMouseEnterHover;
        this.MouseLeave += OnMouseLeaveHover;
    }


    protected void SetBorderColor(bool IsHoverColor)
    {
        if (IsHoverColor)
        {
            this.BorderBrush = new SolidColorBrush(Colors.DarkBlue);
        }
        else
        {
            if(!this.PART_TextBox.IsFocused)
                this.BorderBrush = new SolidColorBrush(Colors.LightGray);
        }
    }


    private void OnMouseLeaveHover(object sender, MouseEventArgs e)
    {
        wTextBox tbObjekt = sender as wTextBox;
        if (tbObjekt != null)
        {
            tbObjekt.SetBorderColor(false);
        }
    }

    private void OnMouseEnterHover(object sender, MouseEventArgs e)
    {
        wTextBox tbObjekt = sender as wTextBox;
        if (tbObjekt != null)
        {
            tbObjekt.SetBorderColor(true);
        }
    }


    /// <summary>
    /// Methode, von jedem Setter einer Property aufgerufen werden muss
    /// </summary>
    /// <param name="propertyName">Name der Property</param>
    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }



    /// <summary>
    /// Methode die ausgeführt wird, wenn der Button gedrückt wird
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void OnButtonClick(object sender, RoutedEventArgs e)
    {

    }

}
}

共有1个答案

仉嘉泽
2023-03-14

好的,对于每个感兴趣的人,这里是我的解决方案。经过一段时间的经验,我发现,当画笔被定义为UserControl的资源时,它是有效的

<UserControl x:Class="Controls.wTextBox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Controls"
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         mc:Ignorable="d" 
         d:DesignHeight="20" d:DesignWidth="150" Height="20" Width="150" BorderThickness="1" BorderBrush="LightGray" x:Name="PART_UserControl" Background="White">
<UserControl.Resources>
    <VisualBrush x:Key="HintTextBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
        <VisualBrush.Visual>
            <Label Foreground="DarkGray" Content="{Binding ElementName=PART_UserControl, Path=HintText}"/>
        </VisualBrush.Visual>
    </VisualBrush>
    <SolidColorBrush x:Key="ClearHinTextBrush" Color="White"/>
</UserControl.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <TextBox Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Center" BorderBrush="Transparent" BorderThickness="0" Name="PART_TextBox">
        <TextBox.Style>
            <Style TargetType="TextBox">
                <Style.Triggers>
                    <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                        <Setter Property="Background" Value="{Binding Source={StaticResource HintTextBrush}}"/>
                    </Trigger>
                    <Trigger Property="Text" Value="{x:Null}">
                        <Setter Property="Background" Value="{Binding Source={StaticResource HintTextBrush}}"/>
                    </Trigger>
                    <Trigger Property="IsKeyboardFocused" Value="True">
                        <Setter Property="Background" Value="{Binding Source={StaticResource ClearHinTextBrush}}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
    <Button Grid.Column="1" Click="OnButtonClick" Width="18" BorderBrush="Transparent" BorderThickness="0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Height="18" Focusable="False">
        <Button.Content>
            <Path Stretch="Fill" Fill="#000000" Margin="2" Data="M29.898 26.5722l-4.3921 0c-0.0118,-0.635 -0.0177,-1.0172 -0.0177,-1.1583 0,-1.4229 0.2352,-2.5929 0.7056,-3.5102 0.4704,-0.9231 1.417,-1.952 2.8281,-3.1044 1.4111,-1.1465 2.2578,-1.8991 2.5282,-2.2578 0.4292,-0.5585 0.6409,-1.1818 0.6409,-1.8579 0,-0.9408 -0.3763,-1.7463 -1.1289,-2.4224 -0.7526,-0.6703 -1.7639,-1.0054 -3.0397,-1.0054 -1.2289,0 -2.2578,0.3527 -3.0868,1.0524 -0.8232,0.6997 -1.3935,1.7698 -1.7051,3.2044l-4.4391 -0.5527c0.1234,-2.0578 0.9995,-3.8041 2.6223,-5.2387 1.6286,-1.4346 3.757,-2.152 6.4029,-2.152 2.7752,0 4.9859,0.7291 6.6322,2.1814 1.6404,1.4522 2.4635,3.1397 2.4635,5.0741 0,1.0642 -0.3057,2.0755 -0.9054,3.028 -0.6056,0.9525 -1.8933,2.2519 -3.8688,3.8923 -1.0231,0.8525 -1.6581,1.5346 -1.905,2.052 -0.2469,0.5174 -0.3587,1.4405 -0.3351,2.7752zm-4.3921 6.5087l0 -4.8389 4.8389 0 0 4.8389 -4.8389 0z"/>
        </Button.Content>
    </Button>
</Grid>

所以不需要任何变通方法。

 类似资料:
  • 如何在Spring Boot中将属性绑定到POJO? POJO类是第三方库类-我不能将@ConfigurationProperties放在那里 我知道一些实用程序类,如DataBinder,可以帮助,但可能有一个更快的方法。提前谢谢你。

  • 想象一下我有一个POJO,比如: 如果我需要使用bind()方法将name成员绑定到JavaFX标签,我不能应用,因为我需要一个可观察值。 我知道我可以使用StringProperty代替String,但是我需要基本类型,因为我使用Hibernate,我不知道Hibernate是否可以支持JavaFX的属性来映射DB中的数据。 我必须将哪些备选方案从我的 pojo 绑定到 JavaFX 控件?

  • 主要内容:Vue.js class,class 属性绑定,实例 1,实例 2,实例 3,实例 4,实例 5,实例 6,,Vue.js style(内联样式),实例 7,实例 8,实例 9Vue.js class class 与 style 是 HTML 元素的属性,用于设置元素的样式,我们可以用 v-bind 来设置样式属性。 Vue.js v-bind 在处理 class 和 style 时, 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。 class 属性绑定 我们可以为

  • 主要内容:Vue.js class,class 属性绑定,实例 1,实例 2,实例 3,实例 4,实例 5,实例 6,,Vue.js style(内联样式),实例 7,实例 8,实例 9,组件上使用 class 属性,实例 10,实例 11Vue.js class class 与 style 是 HTML 元素的属性,用于设置元素的样式,我们可以用 v-bind 来设置样式属性。 v-bind 在处理 class 和 style 时, 表达式除了可以使用字符串之外,还可以是对象或数组。 v-bi

  • 本文向大家介绍aurelia 绑定样式,包括了aurelia 绑定样式的使用技巧和注意事项,需要的朋友参考一下 示例 使用Aurelia绑定到浏览器本机style属性。如果使用字符串插值,则应使用css别名,以便样式在Internet Explorer中起作用。 样式字符串 样式对象 字符串插值 与上面的字符串绑定非常相似,这使您可以使用字符串插值来绑定到样式。如果任何值更改,它们将在视图中进行相

  • 我试图将子类属性绑定到GridViewColumn。我有一个母类M1和三个不同的子类S1、S2和S3。GridViewColumn由类M1的对象填充。我希望将S2的一个属性绑定到这个GridViewColumn的头,而M1中没有实现这个属性。 有人能给我解释一下怎么做吗?