C# 如何绑定到按钮边框厚度?

C# 如何绑定到按钮边框厚度?,c#,xaml,data-binding,windows-8,microsoft-metro,C#,Xaml,Data Binding,Windows 8,Microsoft Metro,我在xaml中为按钮类创建了一个自定义样式。以下是相关部分: <Rectangle Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"/> 显然,这是行不通的,因为虽然冲程厚度是一个双,边界厚度是一个厚度 如何绑定到厚度的实际值(始终是均匀的),而不影响转换器 在您标记为精确副本之前,是不同的。如何 <Window x:Clas

我在xaml中为
按钮
类创建了一个自定义样式。以下是相关部分:

<Rectangle
    Stroke="{TemplateBinding BorderBrush}"
    StrokeThickness="{TemplateBinding BorderThickness}"/>

显然,这是行不通的,因为虽然
冲程厚度
是一个
边界厚度
是一个
厚度

如何绑定到厚度的实际值(始终是均匀的),而不影响转换器

在您标记为精确副本之前,是不同的。

如何

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:Con x:Key="abc" />
    </Window.Resources>
    <Grid>
        <Rectangle StrokeThickness="{Binding abc, Converter={StaticResource abc}}"/>
    </Grid>
</Window>


namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = new { abc = new Thickness(4) };
        }
    }    

    public class Con : IValueConverter
    {
        public object Convert(object value, Type targetType, 
                              object parameter, 
                              System.Globalization.CultureInfo culture)
        {
            return ((Thickness)value).Left;
        }

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

命名空间WpfApplication1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
DataContext=新{abc=新厚度(4)};
}
}    
公共类Con:IValueConverter
{
公共对象转换(对象值,类型targetType,
对象参数,
System.Globalization.culture(信息文化)
{
返回((厚度)值)。左;
}
公共对象转换回(对象值,
类型targetType,
对象参数,
System.Globalization.culture(信息文化)
{
抛出新的NotImplementedException();
}
}
}
怎么样

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:Con x:Key="abc" />
    </Window.Resources>
    <Grid>
        <Rectangle StrokeThickness="{Binding abc, Converter={StaticResource abc}}"/>
    </Grid>
</Window>


namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = new { abc = new Thickness(4) };
        }
    }    

    public class Con : IValueConverter
    {
        public object Convert(object value, Type targetType, 
                              object parameter, 
                              System.Globalization.CultureInfo culture)
        {
            return ((Thickness)value).Left;
        }

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

命名空间WpfApplication1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
DataContext=新{abc=新厚度(4)};
}
}    
公共类Con:IValueConverter
{
公共对象转换(对象值,类型targetType,
对象参数,
System.Globalization.culture(信息文化)
{
返回((厚度)值)。左;
}
公共对象转换回(对象值,
类型targetType,
对象参数,
System.Globalization.culture(信息文化)
{
抛出新的NotImplementedException();
}
}
}
试试这个:

<Rectangle
    Stroke="{TemplateBinding BorderBrush}"
    StrokeThickness="{Binding RelativeSource={RelativeSource TemplatedParent}, 
        Path=BorderThickness.Left}"/>

{TemplateBinding MyProperty}
试试这个:

<Rectangle
    Stroke="{TemplateBinding BorderBrush}"
    StrokeThickness="{Binding RelativeSource={RelativeSource TemplatedParent}, 
        Path=BorderThickness.Left}"/>

{TemplateBinding MyProperty}

可能重复的。。。只是kidding@Rasa当然,在我的收件箱里有一个回复让我兴奋不已:“(…;)你必须加入converter。@NikhilAgrawal我想为这个说法提供更多的证据。可能是。。。只是kidding@Rasa当然,我的收件箱里有一个回复让我兴奋不已:“(…;)你必须使用转换器。@NikhilAgrawal我想为这个说法提供更多的证据。如果没有其他选择,我将不得不使用转换器,但我想先尝试不使用转换器。如果没有其他选择,我将不得不使用转换器,但是我想试着不用一个人先做。啊,这正是我想要的。非常感谢。:)最后,
{TemplateBinding BorderThickness.Left}
也能工作了?啊,这正是我想要的。非常感谢。:)最终,
{TemplateBinding BorderThickness.Left}
也能起作用吗?