2017-07-17 122 views
-3

我创建了一个基本上是应该打开文档的按钮的wpf usercontrol。我想创建它,以便任何人都可以将此文档的URL放入xaml中。这可能吗?WPF UserControl以url作为依赖项属性

编辑:我已经添加了一个依赖属性来存储url,但每当我尝试构建它时都会引发异常。 XAML中看起来是这样的:

<controls:HelpButton WidthAndHeight="40" HelpDocUrl="http://sviamapp.sidmar.be/Documents/Secties/AGP/Applicaties/STL/Procescomputers%20continugieterij/060%20Gebruikersdocumentatie/Gebruik%20kleverdoorbraakbeeld.docx"/> 

后面的属性看起来像这样我的代码:

public string HelpDocUrl  
{ 
    get { return (string)GetValue(HelpDocUrlProperty); } 
    set { SetValue(HelpDocUrlProperty, value); } 
} 

public static readonly DependencyProperty HelpDocUrlProperty = DependencyProperty.Register("HelpDocUrl", typeof(string), typeof(HelpButton), new PropertyMetadata(default(string))); 

回答

1

添加的代码隐藏类的UserControl的依赖属性:

public partial class UserControl1 : UserControl 
{ 
    public UserControl1() 
    { 
     InitializeComponent(); 
    } 

    public static readonly DependencyProperty UrlProperty = 
    DependencyProperty.Register("Url", typeof(string), typeof(UserControl1), new PropertyMetadata(null)); 

    public string Url 
    { 
     get { return (string)GetValue(UrlProperty); } 
     set { SetValue(UrlProperty, value); } 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     string url = Url; 
     //... 
    } 
} 

...您的任何控制消费者可以照常设置:

<local:UserControl1 Url="http://...." /> 
+0

你应该接受这个答案 – Gravity