如何将矩形的RadiusX绑定到矩形的ActualHeight并将其乘以Expression Blend 4(或VS)中的某个数字? [英] How do I bind the RadiusX of a rectangle to the ActualHeight of the rectangle and multiply it by some number in Expression Blend 4 (or VS)?

查看:64
本文介绍了如何将矩形的RadiusX绑定到矩形的ActualHeight并将其乘以Expression Blend 4(或VS)中的某个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在作弊"并使用以下内容:

Right now I am "cheating" and using the following:

<Rectangle x:Name="rectangle" Stroke="SlateGray" 
   Width="{TemplateBinding ActualWidth}" Height="{TemplateBinding ActualHeight}" 
   HorizontalAlignment="Stretch"  VerticalAlignment="Stretch"
   SizeChanged="rectangle_SizeChanged">
</Rectangle>

<x:Code>
  <![CDATA[ private void rectangle_SizeChanged(object sender, SizeChangedEventArgs e)
     {
        Rectangle r = sender as Rectangle;
        r.RadiusX = r.Height / 2;
        r.RadiusY = r.Height / 2;
     }
    ]]>
</x:Code>

x:Code 在运行时完美运行,可以完成我想要的操作.但我真的希望通过以下操作在 Artboard 上立即更改它:

This x:Code works perfectly at run time and accomplishes what I want. but I really want it to change instantly on the Artboard by doing something like:

<Rectangle x:Name="rectangle" Stroke="SlateGray" 
   Width="{TemplateBinding ActualWidth}" Height="{TemplateBinding ActualHeight}" 
   HorizontalAlignment="Stretch"  VerticalAlignment="Stretch"
   RadiusX=".5*({TemplateBinding ActualHeight})"
   RadiusY=".5*({TemplateBinding ActualHeight})"> 
</Rectangle>

但是没有办法包含此 .5 *(...)吗?还有另一种方法可以做到这一点吗?

But there is no way to include this .5*(...) Is there another way to accomplish this?

推荐答案

要在绑定中运行代码,请使用转换器类.

To run code in a binding you use a converter class.

public class MultiplyConverter : IValueConverter 
{ 

  public double Multipler{ get; set; } 

  public object Convert(object value, Type targetType, 
         object parameter, System.Globalization.CultureInfo culture) 
    { 
        double candidate = (double)value; 
        return candidate * Multipler ;
    } 

  public object ConvertBack(object value, Type targetType, 
         object parameter, System.Globalization.CultureInfo culture) 
    { 

        throw new NotSupportedException(); 
    } 
} 

然后在资源"部分中添加转换器.

Then add the converter in a Resources section.

<Window.Resources>
    <local:MultiplyConverter x:Key="MultiplyConverter" Multipler="5"/>
</Window.Resources>

然后将封面添加到您的绑定中.

And add the coverter to your binding.

<Rectangle x:Name="rectangle" Fill="#FFA4A4E4"
        RadiusX="{Binding ActualHeight, Converter={StaticResource MultiplyConverter}, ElementName=rectangle}"
        RadiusY="{Binding ActualWidth, Converter={StaticResource MultiplyConverter}, ElementName=rectangle,}" />

您可以使用Blend绑定窗口自动添加资源和绑定.

You can use the Blend binding windows to automatically add the resource and binding.

这篇关于如何将矩形的RadiusX绑定到矩形的ActualHeight并将其乘以Expression Blend 4(或VS)中的某个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆