繁体   English   中英

在控制模板中编辑模板绑定

[英]Editing Template Binding in control templates

我创建了一个这样的ControlTemplate

<ControlTemplate x:Key="FieldTemplate" TargetType="ContentControl">
  <Border Background="LightGray" >
    <DockPanel >
      <TextBlock DockPanel.Dock="Left" Text="{TemplateBinding c1}" />
      <TextBlock DockPanel.Dock="Left" Text="{TemplateBinding c2}" />
      <TextBox />
    </DockPanel>
  </Border>
</ControlTemplate>

现在我希望能够编辑两个文本块,我该怎么做? 我尝试了类似的方法和其他变体,但没有成功:

<ContentControl c1="hello" c2="olleh" 
       Template="{StaticResource FieldTemplate}" x:Name="NameControl"/>

ContentControl没有c1c2属性。 如果您创建一个自定义控件并将它们定义为依赖项属性,它将起作用:

public class MyControl : ContentControl
{
    public string C1
    {
        get { return (string)GetValue(C1Property); }
        set { SetValue(C1Property, value); }
    }
    public static readonly DependencyProperty C1Property = DependencyProperty.Register(nameof(C1), typeof(string), typeof(MyControl));

    public string C2
    {
        get { return (string)GetValue(C2Property); }
        set { SetValue(C2Property, value); }
    }
    public static readonly DependencyProperty C2Property = DependencyProperty.Register(nameof(C2), typeof(string), typeof(MyControl));
}

XAML:

<ControlTemplate x:Key="FieldTemplate" TargetType="local:MyControl">
    <Border Background="LightGray" >
        <DockPanel >
            <TextBlock DockPanel.Dock="Left" Text="{TemplateBinding C1}" />
            <TextBlock DockPanel.Dock="Left" Text="{TemplateBinding C2}" />
            <TextBox />
        </DockPanel>
    </Border>
</ControlTemplate>
...
<local:MyControl C1="hello" C2="olleh" Template="{StaticResource FieldTemplate}" x:Name="NameControl"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM