2012-06-19 95 views
0

我正在學習WPF,來自Flex和AS,它有時似乎過於複雜。除了意見,我的問題是以下。wpf自定義控件get/set not firing

我創建了一個自定義控件ToolBarButton,它基本上是一個圖像按鈕,它註定要包含在自定義工具欄中。我爲這個控件添加了一些屬性,我希望能夠從XAML中設置它們。雖然該屬性出現在XAML一側的AutoCompletion中,但Set方法始終不會被觸發,並且該屬性保持爲空。因此,這裏的的ToolBarButton代碼背後:

public static readonly DependencyProperty ImgSrcProperty = DependencyProperty.RegisterAttached("ImgSource", typeof(string), typeof(ToolBarButton)); 

    public static readonly DependencyProperty OnClickProperty = DependencyProperty.Register("OnClick", typeof(RoutedEventHandler), typeof(ToolBarButton)); 

    public ToolBarButton(RoutedEventHandler OnClick, string imgSrc, Map map = null, string ConfigFile = null) : 
     base(ConfigFile, map) 
    { 
     if (OnClick != null) SetValue(OnClickProperty, OnClick); 

     if (imgSrc != null) SetValue(ImgSrcProperty, imgSrc); 

     this.AddChild(CreateButton()); 

     InitializeComponent(); 
    } 

    public ToolBarButton() : this(null, null) { } 

    private Button CreateButton() 
    { 
     BitmapImage icon = new BitmapImage(); 
     icon.BeginInit(); 
     icon.UriSource = new Uri(ImgSource, UriKind.Relative); 
     icon.EndInit(); 

     Image img = new Image(); 
     img.Stretch = Stretch.Fill; 
     img.Source = icon; 

     Button BtnToAdd = new Button(); 
     BtnToAdd.Width = 35; 
     BtnToAdd.Height = 35; 
     BtnToAdd.Content = img; 
     BtnToAdd.Background = new ImageBrush(icon); 

     BtnToAdd.Click += OnClick; 

     return BtnToAdd; 
    } 


    public string ImgSource 
    { 
     get { return (string)GetValue(ImgSrcProperty); } 
     set { SetValue(ImgSrcProperty, value); } 
    } 

    public RoutedEventHandler OnClick 
    { 
     get { return (RoutedEventHandler)GetValue(OnClickProperty); } 
     set { SetValue(OnClickProperty, value); } 
    } 

你會注意到兩個構造,一個在運行時創建控件,另從XAML創建它。

這裏是一個使用自定義的控制,但不火的設置方法的XAML代碼:

<BaseControls:ToolBar 
     x:Class="Basic_Mapping.Widgets.NavigationToolBar" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:BaseControls="clr-namespace:Basic_Mapping.Base_Controls" 
     mc:Ignorable="d" > 

<BaseControls:ToolBarButton Width="35" Height="35" ImgSource="Assets/i_zoomin.png" ConfigFileName="ZoomIn.xml" /> 

任何幫助,將不勝感激!

Ggilmann

編輯:

下面是用於的ToolBarButton的基類,它也有同樣的問題,它的屬性。

public partial class ConfigurableUserControl : UserControl 
{ 
    private XmlDocument configXML; 

    public static readonly DependencyProperty XmlProperty = DependencyProperty.Register("ConfigFileName", typeof(string), typeof(ConfigurableUserControl)); 

    public static readonly DependencyProperty MapProperty = DependencyProperty.Register("Map", typeof(Map), typeof(ConfigurableUserControl)); 

    public ConfigurableUserControl(string configFile, Map map) 
    { 
     if (configFile != null) SetValue(XmlProperty, configFile); 

     if (map != null) SetValue(MapProperty, map); 

     string file = (string)GetValue(XmlProperty); 

     if (file != null) 
     { 
      configXML = new XmlDocument(); 

      string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\Config\\" + configFile); 

      if (File.Exists(path)) configXML.Load(path); 
     } 
    } 

    public ConfigurableUserControl() : this(null, null) { } 

    public string ConfigFileName 
    { 
     //get { return (string)GetValue(XmlProperty); } 
     set { SetValue(XmlProperty, value); } 
    } 

    public Map Map 
    { 
     get { return (Map)GetValue(MapProperty); } 
     set { SetValue(MapProperty, value); } 
    } 

    public XmlDocument ConfigXML 
    { 
     get { return configXML; } 
    } 
} 
+0

您的ImgSrcProperty是一個附加屬性,它不應該是。你能列出這個派生出來的基類嗎?你可以添加一個OnPropertyChanged事件來查看是否被觸發(它應該),因爲DependecyProperties的getter和setter並不總是以某些調試/編譯設置觸發。 – Kolky

+0

我忘了說我也嘗試過使用公共變量的基本get/set屬性,但它沒有幫助。我將編輯原始帖子以顯示基類 – Ggilmann

回答

0

我的猜測是,這個問題和你的基類問題是由於你沒有實現INotifyPropertyChanged並進行適當的調用。

+0

的確,我沒有實現INotifyPropertyChanged,我會進一步研究這一點。 – Ggilmann

+0

好吧,似乎我有它的工作,現在我只需要弄清楚電話的順序。看來構造函數在屬性設置之前被調用......我想我必須添加一個事件偵聽器到InitializationComplete或其他東西。無論如何,感謝您幫助解決問題! – Ggilmann

+0

我會更正我的代碼並將其發回,讓其他人看到 – Ggilmann

0

嘗試把InitializeComponent();在構造函數的開始,因爲在目前它是結束反對。

+0

它似乎沒有幫助 – Ggilmann

+0

我忘了指出,set方法可能不會被解僱,因爲XAML可以調用依賴對象方法'GetValue'和'SetValue'直。 –

+0

我不確定我應該如何處理這些信息。 – Ggilmann