Reputation:
I'm new in WPF, but not in programming, I'm trying to find Attached Properties like ZIndex, Canvas.Top, Canvas.Left but it doesn't show me in C# code.
I can see all those properties in my XAML code, but cant reach them with my C# Code
<Button Canvas.Left="192" Canvas.Top="102" Content="Button" Height="108" Name="button1" Width="173" /> <- XAML Part
button1.Canvas.Top = 5; <- C# Part
it says button1 doesn't have any Canvas.Top method or Attribute.
Upvotes: 1
Views: 169
Reputation: 35721
c# equivalent of xaml Canvas.Top
attribute will be Canvas.TopProperty
. Canvas.Top
doesn't exist in c# code; in xaml it is recognized due to attached DP naming conventions.
Canvas.TopProperty can be set to any DependencyObject using SetValue
method:
button1.SetValue(Canvas.TopProperty, 5.0);
Canvas.SetTop()
internally uses SetValue()
with Canvas.TopProperty
. Canvas.SetTop()
has an advantage of being type-safe. It accept double
parameters, not object
. E.g. button1.SetValue(Canvas.TopProperty, 5);
throws ArgumentException, because 5
is int
Upvotes: 0
Reputation: 560
The Button
itself doesn't have a Canvas
. This should do it:
Canvas.SetTop(button1, 5);
and then:
canvas.Children.Add(button1);
Upvotes: 3