2010-07-05 70 views
6

我在WPF用戶控件,我想要的它的一個從它使用XAML讀取標籤的文本。因此..設置XAML屬性值的用戶控制

我的用戶控件:

<UserControl x:Class="muc"> 
     <Label Foreground="#FF7800" FontSize="20" FontWeight="Bold">   
      <Label.Content> 
       <Binding ElementName="TestName" Path="." /> 
      </Label.Content> 
     </Label> 
</UserControl> 

然後使用它:

<mycontorls:muc TestName="This is a test" /> 

但它沒有作品... 我怎樣才能讀取性能?

+1

所有你想要的是一個名爲TestName的依賴屬性,必須在UserControl的代碼隱藏中創建,如AlvinfromDiaspar所描繪的。然後,您可以使用ElementName(Quartermeister 的答案)或RelativeSource綁定(Pavel Minaev的答案)將Label的內容綁定到UserControl的TestName屬性。 – Amsakanna 2010-07-06 08:30:57

回答

3

我只用Silverlight的做到了這一點,但如果它工作在完全相同的方式,我不會是驚訝!

// <summary> 
// Xaml exposed TextExposedInXaml property. 
// </summary> 
public static readonly DependencyProperty TestNameProperty = DependencyProperty.Register("TestName", typeof(string), typeof(NameOfMyUserControl), new PropertyMetadata(string.empty)); 

// <summary> 
// Gets or sets the control's text 
// </summary> 
public string TextExposedInXaml 
{ 
      get 
      { 
       return (string)GetValue(TestNameProperty); 
      } 

      set 
      { 
       SetValue(TestNameProperty , value); 

       // set the value of the control's text here...! 
      } 
} 
4

如果你給根用戶控件元素的名稱,那麼你可以參考它使用的ElementName:

<UserControl x:Class="muc" 
      Name="rootElement"> 
    <Label Foreground="#FF7800" FontSize="20" FontWeight="Bold"> 
     <Label.Content> 
      <Binding ElementName="rootElement" Path="TestName" /> 
     </Label.Content> 
    </Label> 
</UserControl> 

您也可以使用標記擴展語法,使之短一點:

<UserControl x:Class="muc" 
      Name="rootElement"> 
    <Label Foreground="#FF7800" FontSize="20" FontWeight="Bold" 
      Content="{Binding TestName, ElementName=rootElement}"/> 
</UserControl> 

另外請記住,您的控件將在其屬性設置之前創建。你要麼需要執行INotifyPropertyChanged或有TestName是一個依賴屬性,以使屬性設置後綁定重新評估。

0

{Binding ElementName=x}結合的元素與名x在元素樹,還有這裏沒有什麼財產TestName交易。如果你想在用戶控件的屬性,那麼你必須定義對應於用戶控制類的屬性(在你的情況下,將muc),並使用{Binding RelativeSource={RelativeSource FindAncestor, ...}}引用它在你的用戶控件(參見here詳細信息),或給它一個名稱,以便您可以使用ElementName

8

我嘗試的第一個兩個答案和我得到了什麼工作的代碼,但不是在XAML(也不允許您使用該控件時看到在設計視圖中的變化)。

要獲取屬性像任何其他原生一個工作,這裏是全過程: (該示例將類型可空的依賴屬性對照文本或默認,如果空顯示)

  1. 在代碼文件中:

    1.A定義依賴屬性:

    public static readonly DependencyProperty MyNumberProperty = DependencyProperty.Register("MyNumber", typeof(Nullable<int>), typeof(MyUserControl), new PropertyMetadata(null, new PropertyChangedCallback(OnMyNumberChanged))); 
    

    1.B落實OnMyNumberChanged回調:

    private static void OnMyNumberChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args){ 
        // When the color changes, set the icon color PlayButton 
        MyUserControl muc = (MyUserControl)obj; 
        Nullable<int> value = (Nullable<int>)args.NewValue; 
        if (value != null) 
        { 
         muc.MyNumberTextBlock.Text = value.ToString(); 
        } 
        else 
        { 
         muc.MyNumberTextBlock.Text = "N/A"; 
        } 
    } 
    

    1。ç實施mynumber的財產(不依賴)在代碼中使用使用依賴屬性,方便:

    public Nullable<int> MyNumber{ 
        get 
        { 
         return (Nullable<int>)GetValue(MyNumberProperty); 
        } 
        set 
        { 
         SetValue(MyNumberProperty, value); 
         OnTargetPowerChanged(this, new DependencyPropertyChangedEventArgs(TargetPowerProperty, value, value)); // Old value irrelevant. 
        } 
    } 
    
  2. 在XAML文件綁定TextBlock控件的文本屬性(不依賴)獲得的默認值如果它不被控制的用戶設置的依賴屬性(假設你叫用戶控件的根元素「rootElement的」):

此代碼:

< TextBlock Name="MyNumberTextBlock" Text="{Binding MyNumber, ElementName=RootElement}"/>