2013-06-21 22 views
1

我正在創建一個應用程序,用於在單個窗口中編輯大量產品規格。使用DataTemplate在ContentControl中指定靜態文本值

我有一堆尺寸(以英寸爲單位),我想創建一個簡單的模板,它將顯示每個維度的值作爲分數和小數值。它基本上是一個TextBlock和兩個TextBox。

inches control UI

但我想不出如何指定TextBlock的文本(在這種情況下,寬度)。
我想能夠在ContentControl聲明(或類似的東西)中指定它。

這裏是我的DataTemplate:

<Window.Resources> 
    <DataTemplate x:Key="InchesInputTemplate"> 
     <StackPanel> 
      <TextBlock Text="{Binding}" /> <!-- How should I define the binding ? --> 
      <TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=ContentControl}, Path=Content, Converter=InchesToFractionConverter}" /> 
      <TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=ContentControl}, Path=Content}" /> 
     </StackPanel> 
    </DataTemplate> 
</Window.Resources> 

,然後我用它在ContentControl中:

<ContentControl Content="{Binding Width}" 
       ContentTemplate="{StaticResource InchesInputTemplate}" 
       LabelText="Width :" /> 

我的簡化產品類(它將包含更多的維度):

public class Product 
{ 
    private string _productCode; 

    public string ProductCode 
    { 
     get { return _productCode; } 
     set { _productCode = value; } 
    } 

    private float _width; 

    public float Width 
    { 
     get { return _width; } 
     set { _width = value; } 
    } 
} 

什麼是指定我的每個維度的Label的文本的最佳方式(LabelText pr在我的例子operty)?

+0

您綁定的英寸屬性在哪裏?浮點寬度屬性與您綁定到內容的屬性不同嗎?如果你傳遞一個複雜的對象(帶有一個英寸屬性)作爲內容,你可以添加更多的屬性,並綁定到模板內。 –

+0

@John Bowen你說得對,我在這裏犯了一個錯誤。起初我想爲我的維度製作一個複雜的對象,但我意識到這不是必需的。我糾正了綁定表達。 – igelineau

+1

這樣做更有意義,但新的綁定是不必要的複雜。父控件的內容已經是模板中的DataContext,因此您可以使用{Binding Converter = InchesToFractionConverter} –

回答

2

您可以使用Tag財產

<DataTemplate x:Key="InchesInputTemplate"> 
    <StackPanel> 
    <TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentControl}}, Path=Tag}" /> 
    <!-- How should I define the binding ? --> 
    <TextBox Text="{Binding Inches, Converter=InchesToFractionConverter}" /> 
    <TextBox Text="{Binding Inches}" /> 
    </StackPanel> 
</DataTemplate> 

<ContentControl Content="{Binding Width}" 
       ContentTemplate="{StaticResource InchesInputTemplate}" 
       Tag="Width :" /> 

更新:

如果你不想使用Tag屬性,你可以使用一個Attached Property

public class MyLabelPropertyClass { 
    public static readonly DependencyProperty MyLabelTextProperty = 
    DependencyProperty.RegisterAttached(
     "MyLabelText", 
     typeof(string), 
     typeof(MyLabelPropertyClass), 
     new FrameworkPropertyMetadata(
     string.Empty, FrameworkPropertyMetadataOptions.Inherits)); 

    public static void SetMyLabelText(UIElement element, string value) { 
    element.SetValue(MyLabelTextProperty, value); 
    } 

    public static string GetMyLabelText(UIElement element) { 
    return (string)element.GetValue(MyLabelTextProperty); 
    } 
} 

<DataTemplate x:Key="InchesInputTemplate"> 
    <StackPanel> 
    <TextBlock Text="{Binding Path=(local:MyLabelPropertyClass.MyLabelText), RelativeSource={RelativeSource Self}}" /> 
... 
</DataTemplate> 
... 
<ContentControl Content="{Binding Width}" 
       ContentTemplate="{StaticResource InchesInputTemplate}" 
       local:MyLabelPropertyClass.MyLabelText="Width :" /> 

備用

如果你想繼承ContentControl與正常Dependency property

public class MyCustomContentControl : ContentControl { 
    public static readonly DependencyProperty MyLabelTextProperty = 
    DependencyProperty.Register(
     "MyLabelText", 
     typeof(string), 
     typeof(MyCustomContentControl), 
     new FrameworkPropertyMetadata(string.Empty)); 

    public string MyLabelText { 
    get { 
     return (string)GetValue(MyLabelTextProperty); 
    } 
    set { 
     SetValue(MyLabelTextProperty, value); 
    } 
    } 
} 

<DataTemplate x:Key="InchesInputTemplate"> 
    <StackPanel> 
    <TextBlock Text="{Binding Path=MyLabelText, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyCustomContentControl}}}" /> 
... 
</DataTemplate> 
... 
<local:MyCustomContentControl ContentTemplate="{StaticResource InchesInputTemplate}" 
           MyLabelText="Width :" /> 
+0

謝謝!好主意,它會工作,但我更喜歡避免使用標籤。如果它真的是解決這個問題的最好方法,我仍然可以使用它(但我希望有一種更自然的WPF方法來實現它)。 – igelineau

+0

@igelineau嗯,這是最快的,但如果你不想使用'Tag'屬性,那麼你可以使用附加屬性(我已經用示例更新了我的答案)。如果你想進一步區分,可以子類ContentControl,併爲它添加一個Dependency屬性。 – Viv

+0

使用依賴屬性對ContentControl進行子類化很有意思,它似乎是迄今爲止更簡潔的方法。我可能會在週末後試試這個。謝謝 ! – igelineau

相關問題