繁体   English   中英

C#XAML WPF在UserControl上呈现之前使用参数

[英]C# XAML WPF Use Parameters before Render on UserControl

我是c#世界的新手,对xaml UserControl对象的参数使用有疑问。

我在xaml的MainGrid / Main窗口中定义了一个UserControll“ImageButton”:

<local:ImageButton HorizontalAlignment="Left" Height="100" Margin="49,122,0,0" VerticalAlignment="Top" Width="100" sText="SomeText" sType="main_button" Source="Resources/main_button.png" />

另一方面,我有我的ImageBButton.xmal.cs

public partial class ImageButton : UserControl
.....
public ImageSource Source
{
    get { 
        return (ImageSource)GetValue(SourceProperty); }
        set { SetValue(SourceProperty, value); }
    }
.....
public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(default(ImageSource)));
.....
public ImageButton()
{
    InitializeComponent();
}
.....

现在我想知道我可以通过xaml中定义的参数值访问哪一点。

我尝试了几种方法(包括构造函数本身),但我只在后面的c#代码中得到一个空值。 我现在尝试了几种方法,现在使用“OnRender”方法 - 在这种方法中,我可以从xaml访问我的参数值。

但我真的不确定这是不是正确的方法..

可能有人在绘制Usercontroll之前知道另一个方法,在那里我可以访问xaml参数值并处理一些事情吗?

亲切的问候

在UserControl自己的Xaml中声明的属性值将在InitializeComponent() 在引发Initialized之后,UserControl的Xaml使用站点中提供的值将可用。

绑定有点不同; 将根据上述规则应用绑定并准备接收值,但在调度程序有机会处理具有DataBind优先级的项目之前,源值可能不会传输到目标。 这将在控件Loaded

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM