2011-08-15 48 views
0

我想到了一個想法,它簡短地解釋說我想加載xaml-files運行時,然後將它們綁定到運行時數據。 內這些XAML的文件,我會用一個叫做「PropertySetter」這樣的成分:綁定到運行時加載的XAML

public class PropertySetter : UserControl 
{ 
    public string Value { get; set; } 
    public string Text { get; set; } 
    public string Adress { get; set; } 

    public PropertySetter() 
    { 
    } 
} 

而且xamlfile,應該是這樣的:

<Grid 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:vf="clr-namespace:VisualFactory.vfGraphics.Graphics;assembly=VisualFactory"> 
    <vf:PropertySetter Name="Datapoint" Text="propset" Value="false" Adress="10201" /> 
    <CheckBox IsChecked="{Binding ElementName=Datapoint, Path=Value}" Content="{Binding ElementName=Datapoint, Path=Text}" /> 
</Grid> 

我希望你現在明白了吧。 該複選框會將其值(內容,缺陷)綁定到屬性設置器,並且當應用程序運行時,它只會更改一堆「PropertySetter」的值,圖形將更新其內容和值。 但是:雖然xaml在加載時正確設置了值,但當我更改propertysetters的值時,它並不更新它們。我嘗試過設置Mode = TwoWay,而物業公司則位於iNotify集合中。 有沒有人嘗試過這樣的事情?

回答

0

答案奠定在 「的DependencyProperty」 級。我改變了PropertySetter這樣:

public class PropertySetter : UserControl 
    { 
     public string Value 
     { 
      get { return (string)GetValue(ValueProperty); } 
      set { SetValue(ValueProperty, value); } 
     } 

     public static readonly DependencyProperty ValueProperty = 
      DependencyProperty.Register("Value", typeof(string), typeof(PropertySetter), new UIPropertyMetadata("0")); 




     public string Text 
     { 
      get { return (string)GetValue(TextProperty); } 
      set { SetValue(TextProperty, value); } 
     } 

     public static readonly DependencyProperty TextProperty = 
      DependencyProperty.Register("Text", typeof(string), typeof(PropertySetter), new UIPropertyMetadata("def")); 




     public string Adress 
     { 
      get { return (string)GetValue(AdressProperty); } 
      set { SetValue(AdressProperty, value); } 
     } 

     public static readonly DependencyProperty AdressProperty = 
      DependencyProperty.Register("Adress", typeof(string), typeof(PropertySetter), new UIPropertyMetadata("")); 



     public PropertySetter():base() 
     { 
     } 
    } 

瞧,這一切都正確地結合,現在我已經在運行時加載XAML和app-邏輯之間的層。

0

你需要做的是在你的PropertySetter類上實現INotifyPropertyChanged並在每個屬性的setter中觸發PropertyChanged事件。這是綁定知道更新源對源更改UI的機制。

class PropertySetter :UserControl,INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    private string address; 
    private string text; 
    private string value; 

    public string Value 
    { 
     get { return value; } 
     set 
     { 
      if (value == value) 
       return; 
      value = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("Value")); 
     } 
    } 

    public string Text 
    { 
     get { return text; } 
     set 
     { 
      if (text == value) 
       return; 
      text = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("Text")); 
     } 
    } 

    public string Address 
    { 
     get { return address; } 
     set 
     { 
      if (address == value) 
       return; 
      address = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("Address")); 
     } 
    } 

    public PropertySetter() 
    { 

    } 
} 

PS:TwoWay數據綁定,即用來告訴數據綁定,當改變在目標製成(在你的情況下,CheckBox的屬性),這些都反映在來源(在你的情況下,PropertySetter的屬性)。

希望這有助於:)

+0

嗨AbdouThank的答案,但我已經嘗試過一個無果:) – Filip

+0

但我在這裏找到答案:http://www.wpftutorial.net/DependencyProperties.html#creation 我只需要使用propertysetter類中的DependencyProperty。 – Filip

+0

哦!是的,那將是我的第一個迴應,而且確實是這個問題,因爲你在XAML中設置了值,但是然後我讀了這個問題並忘記了這一點,只看到你沒有做更改通知。不過,我確定爲什麼你需要在XAML中創建'PropertySetter'(我還打算添加該註釋) – AbdouMoumen