【问题标题】:How to bind to Button BorderThickness?如何绑定到 Button BorderThickness?
【发布时间】:2012-09-25 10:55:47
【问题描述】:

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

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

这显然行不通,因为StrokeThicknessdoubleBorderThicknessThickness

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

在标记为完全重复之前,this question 是不同的。

【问题讨论】:

  • stackoverflow.com/questions/1935416/… 的可能重复项...开个玩笑
  • @Rasa 当然,收件箱中的回复让我兴奋不已:'( ... ;)
  • 你必须进入转换器。
  • @NikhilAgrawal 我想为这个说法提供更多证据。

标签: c# xaml data-binding windows-8 microsoft-metro


【解决方案1】:

试试这个:

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

注意

以下绑定

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

相同
{TemplateBinding MyProperty}

【讨论】:

  • 啊,这正是我想要的。非常感谢。 :)
  • 最终{TemplateBinding BorderThickness.Left} 也可以使用?
【解决方案2】:

怎么样

<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();
        }
    }
}

【讨论】:

  • 如果没有其他选择,我将不得不使用转换器,但我想先尝试不使用转换器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
  • 1970-01-01
  • 1970-01-01
  • 2016-04-01
相关资源
最近更新 更多