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,853 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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>
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.