Wpf 将值传递给控件模板依赖项属性

Wpf 将值传递给控件模板依赖项属性,wpf,control-template,Wpf,Control Template,如何在从自定义控件定义中获取的控件模板值中设置CornerRadious的值。。。请帮忙 用户控件的XAML是 <UserControl x:Class="Biz10.RoundedCornerTextBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/win

如何在从自定义控件定义中获取的控件模板值中设置CornerRadious的值。。。请帮忙 用户控件的XAML是

<UserControl x:Class="Biz10.RoundedCornerTextBox"
                 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:Biz10"
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
        <UserControl.Resources>
            <ControlTemplate x:Key="TextBoxBaseControlTemplate" TargetType="{x:Type TextBoxBase}">
                <Border Background="{TemplateBinding Background}" 
                    x:Name="Bd" BorderBrush="Gray"
                    BorderThickness="1" **CornerRadius="5"**>
                    <ScrollViewer x:Name="PART_ContentHost"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" TargetName="Bd"/>
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                    <Trigger Property="Width" Value="Auto">
                        <Setter Property="MinWidth" Value="100"/>
                    </Trigger>
                    <Trigger Property="Height" Value="Auto">
                        <Setter Property="MinHeight" Value="20"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </UserControl.Resources>
        <Grid>
            <TextBox Template="{StaticResource TextBoxBaseControlTemplate}" Height="25" Margin="5"></TextBox>
        </Grid>
    </UserControl>

C#代码文件为

using System.Windows.Media.Imaging;
using System.Windows.Navigation;

using System.Windows.Shapes;

namespace Biz10
{
    /// <summary>
    /// Interaction logic for CornerTextBox.xaml
    /// </summary>
    public partial class RoundedCornerTextBox : UserControl
    {
        public static readonly DependencyProperty _cornrerRadious= DependencyProperty.Register("CornerRadious", typeof(int), typeof(RoundedCornerTextBox));
    public RoundedCornerTextBox()
    {
        InitializeComponent();
    }
    public int CornerRadious
    {
        get
        {
            return (int)GetValue(_cornrerRadious);
        }
        set
        {
            SetValue(_cornrerRadious, value);
        }
    }
}
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
名称空间Biz10
{
/// 
///CornerTextBox.xaml的交互逻辑
/// 
公共部分类RoundedCornerTextBox:UserControl
{
public static readonly dependencProperty\u cornrerrious=dependencProperty.Register(“CornerRadious”、typeof(int)、typeof(RoundedCornerTextBox));
公共圆角文本框()
{
初始化组件();
}
公共广播电台
{
得到
{
返回(int)GetValue(_cornrerrious);
}
设置
{
设置值(_cornrerrious,value);
}
}
}
}

我想在Window by中声明一个自定义控件

<custome:RoundedCornerTextBox CornerRadious="7" Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="7" x:Name="txtAccountName" ></custome:RoundedCornerTextBox>


是否可能

您必须更改
用户控件的
模板
,如下所示:

<UserControl.Template>
    <ControlTemplate TargetType="UserControl">
        <Border BorderThickness="3" BorderBrush="#FFF0B0B0" CornerRadius="{Binding CornerRadius, RelativeSource={RelativeSource AncestorType=UserControl}}">
            <Grid>
                <TextBox Width="100"/>
            </Grid>
        </Border>
    </ControlTemplate>
</UserControl.Template>


查看如何绑定
CornerRadius
属性。

您必须更改
用户控件的
模板
,如下所示:

<UserControl.Template>
    <ControlTemplate TargetType="UserControl">
        <Border BorderThickness="3" BorderBrush="#FFF0B0B0" CornerRadius="{Binding CornerRadius, RelativeSource={RelativeSource AncestorType=UserControl}}">
            <Grid>
                <TextBox Width="100"/>
            </Grid>
        </Border>
    </ControlTemplate>
</UserControl.Template>


查看如何绑定
CornerRadius
属性。

您不需要封闭的用户控件。只需创建一个名为RoundedCornerTextBox的自定义控件()

  • 扩展文本框
  • 将其模板更改为您需要的模板
  • 公开新的CornerRadius依赖项属性,并最终
  • 使用TemplateBinding将此属性连接到模板内的边框

  • 您不需要封闭的UserControl。只需创建一个名为RoundedCornerTextBox的自定义控件()

  • 扩展文本框
  • 将其模板更改为您需要的模板
  • 公开新的CornerRadius依赖项属性,并最终
  • 使用TemplateBinding将此属性连接到模板内的边框

  • 对这显然是可能的,是的。这显然是可能的。