我正在创建一个应用程序,用于在一个窗口中编辑大量产品规范。
我有一堆维度(以英寸为单位),我想创建一个简单的模板,为每个维度显示值作为分数和小数值。 它基本上是一个TextBlock和两个TextBox。
但我无法弄清楚如何指定TextBlock的文本(在本例中为Width) 我希望能够在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 :" />
我简化的Product类(它将包含更多维度):
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属性)?
答案 0 :(得分: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 :" />