0

I was wondering if in XAML without touching the view model I could do something like this or this except use a ratio of the other property.

I have a button control with 2 ellipses inside and I want the margin of one of the ellipses to vary depending on the height of the other.

So something like:

<Ellipse Margin=.2*"{Binding ElementName=OtherEllipse, Path=Height}"/>
Community
  • 1
  • 1
azulBonnet
  • 831
  • 4
  • 14
  • 31

2 Answers2

0

You can, you need to write custom IValueConverter. http://www.codeproject.com/Tips/868163/IValueConverter-Example-and-Usage-in-WPF

And if you need to pass a parameter: Passing values to IValueConverter

Community
  • 1
  • 1
Filip
  • 1,824
  • 4
  • 18
  • 37
0

MainWindow.xaml

<Window x:Class="MultiBindingConverterDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:MultiBindingConverterDemo"
    mc:Ignorable="d"
    Title="MainWindow" Height="600" Width="800">
<StackPanel>
    <StackPanel.Resources>
        <local:MultiplyValueConverter x:Key="MultiplyValueConverter"/>
    </StackPanel.Resources>
    <Ellipse x:Name="OtherEllipse" Width="100" Height="50" Fill="Red"/>
    <Ellipse Width="50" Height="50" Fill="Blue" 
             Margin="{Binding Path=Height, 
                              ElementName=OtherEllipse, 
                              Converter={StaticResource MultiplyValueConverter}, 
                              ConverterParameter=0.2}">
    </Ellipse>
</StackPanel>

MainWindow.xaml.cs

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace MultiBindingConverterDemo
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MultiplyValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double height = (double)value;
            double multiplier = double.Parse((string)parameter);
            return new Thickness(height * multiplier);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
Wiesław Šoltés
  • 3,114
  • 1
  • 21
  • 15
  • Thank you!! I still can't seem to get it to work may have something to do with it being in a style but I'm working on it! – azulBonnet Feb 05 '16 at 18:13