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)?
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
works perfectly at run time and accomplishes what I want. but I really want it to change instantly on the Artboard
делая что-то вроде:
<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>
But there is no way to include this .5*(...)
Есть ли другой способ сделать это?
1 ответ
Для запуска кода в привязке вы используете класс конвертера.
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();
}
}
Затем добавьте конвертер в раздел Ресурсы.
<Window.Resources>
<local:MultiplyConverter x:Key="MultiplyConverter" Multipler="5"/>
</Window.Resources>
И добавьте обложку к вашему переплету.
<Rectangle x:Name="rectangle" Fill="#FFA4A4E4"
RadiusX="{Binding ActualHeight, Converter={StaticResource MultiplyConverter}, ElementName=rectangle}"
RadiusY="{Binding ActualWidth, Converter={StaticResource MultiplyConverter}, ElementName=rectangle,}" />
Вы можете использовать окна Blend binding для автоматического добавления ресурса и привязки.