0

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

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

显然这是行不通的,因为 whileStrokeThickness是 a doubleBorderThickness是 a Thickness

我怎样才能绑定到厚度的实际值(这将始终是统一的),而不会弄乱转换器?

在您标记为完全重复之前,这个问题是不同的。

4

2 回答 2

5

试试这个:

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

笔记

以下绑定

{Binding RelativeSource={RelativeSource TemplatedParent}, Path=MyProperty}

是相同的

{TemplateBinding MyProperty}
于 2012-10-04T23:28:11.950 回答
0

怎么样

<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();
        }
    }
}
于 2012-10-04T12:21:50.977 回答