开发者

WPF: Access XAML objects/shapes/path declared using C#

I'm a newbie in WPF and I would like to ask if it's possible to access my WPF ojects using C#.

Here is a line of code from WPF:

<Path x:Name="layout1"
      Fill="Red" Stretch="Fill" Stroke="Red"
      HorizontalAlignment="Left" Margin="374.714,140.786,0,0"
      VerticalAlignment="Top" Width="116.215" Height="109.571"
      Data="M374.71429,204.14286 L387.07172,249.357 489.9328,15开发者_C百科7.92762 451.36006,140.78486 428.50213,157.92762 409.21576,173.64206 390.6437,189.35651 z"
      />

How would I access 'layout1' (using C#) in such a way that I could change its visibility into 'hidden'?


Very simple:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    layout1.Visibility = System.Windows.Visibility.Hidden;
}

in any code-behind method.


The path is automatically accessible in the code-behind file, using the name specified in x:Name, in this case layout1. If you'd like to also access it from a place other than the code-behind file, you should add

x:FieldModifier="public"

to the tag. This way, it get a publicly accessible attribute for the object the path is in. Again, you access it through the name specified in x:Name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜