How do use MultiBinding for the attached property?

奇 卢 181 Reputation points
2021-07-05T12:05:32.287+00:00

I want to place a Button inside the Canvas and I need to bind the Button "Canvas.Top" Attached property to its own Width property and Height property. How I should build XAML statements?

<Canvas>
  <Button>
    ...
    ...
  </Button>
</Canvas>
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,621 Reputation points
    2021-07-06T02:09:39.227+00:00

    I made a sample for your question based on my understanding, if I misunderstand, please point out.
    XAML:

      <Window.Resources>  
            <local:ValueConverter x:Key="addValue"></local:ValueConverter>  
        </Window.Resources>  
        <Canvas>  
            <Button Name="btn" Height="20" Width="200">  
                <Canvas.Top>  
                    <MultiBinding Converter="{StaticResource addValue}">  
                        <Binding ElementName="btn" Path="Width"/>  
                        <Binding ElementName="btn" Path="Height"/>  
                    </MultiBinding>  
                </Canvas.Top>  
            </Button>  
        </Canvas>  
    

    C#:

     public class ValueConverter : IMultiValueConverter  
        {  
            public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)  
            {  
                if (values == null || values.Length == 0)  
                    throw new ArgumentNullException("values can not be null");  
                double addedValue = 0.0;  
                foreach(var item in values)  
                {  
                    addedValue += System.Convert.ToDouble(item);  
                }  
                return addedValue;  
            }  
      
            public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)  
            {  
                return null;  
            }  
        }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful