Access XAML object from code

Access XAML object from code

野心澎湃 发布于 2021-11-28 字数 826 浏览 894 回复 2 原文

I created a UserControl in WPF:

In Xaml:

<UserControl x:Class="OutlookPanel.MailRelation"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300"
    xmlns:graph="clr-namespace:MyPanel"
>
 <DockPanel>
<graph:Graph Name="theGraph" NodesBindingPath="ChildNodes"
               NodeTemplateSelector="{StaticResource nodeTemplateSelector}">
..
 </DockPanel>
</UserControl>

I cs:

object theThing = e.Parameter;
                    ((MailRelation)sender).theGraph.CenterObject = theThing;

This last sentence does not work as theGraph is not accessible.
Any idea why i can access theGraph ?

Thanks

John

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妞丶爷亲个 2022-06-07 2 楼

Probably because that property is private. Provide a public getter and you should be able to get it. In your code, add something like

 public Graph TheGraph  { get { return theGraph; } }
静谧 2022-06-07 1 楼

Name="theGraph"

should be

x:Name="theGraph"

from http://msdn.microsoft.com/en-us/library/ms752059.aspx

x:Name: Specifies a run-time object
name for the instance that exists in
run-time code after an object element
is processed. You use x:Name for cases
of naming elements where the
equivalent WPF framework-level Name
property is not supported. This
happens in certain animation
scenarios.