Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何使用TemplateBinding继承控件的所有属性_Wpf_Xaml_Datatemplate - Fatal编程技术网

Wpf 如何使用TemplateBinding继承控件的所有属性

Wpf 如何使用TemplateBinding继承控件的所有属性,wpf,xaml,datatemplate,Wpf,Xaml,Datatemplate,我正在尝试制作自己版本的控件,比如说文本框,我想在其中添加一些自定义UI。我想从这个类继承,这样用户仍然可以像调用普通文本框一样调用我的特殊文本框。我有以下资料: // MySpecialTextBox.cs public class MySpecialTextBox : TextBox { static MySpecialTextBox () { } /* Some special properties */ } 在XAML中: <Style Tar

我正在尝试制作自己版本的控件,比如说
文本框
,我想在其中添加一些自定义UI。我想从这个类继承,这样用户仍然可以像调用普通文本框一样调用我的特殊文本框。我有以下资料:

// MySpecialTextBox.cs

public class MySpecialTextBox : TextBox
{
    static MySpecialTextBox () { }   

    /* Some special properties */     
}
在XAML中:

<Style TargetType="{x:Type MySpecialTextBox}">        
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MySpecialTextBox}">
                <StackPanel>
                    <Rectangle Width="100" Height="3" Fill="Pink" /> <!-- My own fancy lay-out -->
                    <TextBox Text="{TemplateBinding Text}"/>         <!-- This text box should 'inherit' ALL the properties from the caller -->
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

列出所有属性,但这感觉效率极低。有没有一种方法可以一次绑定到整个父对象?

将另一个
TextBox
元素放置在另一个
TextBox
的模板中不是一个好主意。当覆盖控件的模板时,应该始终编辑其默认模板

因此,在
文本框的默认模板中
可以找到以下元素:

    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
        <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
    </Border>

TemplateBinding
是标准
Binding
的优化形式,大致相当于使用
Binding
,将
RelativeSource
属性设置为
RelativeSource.TemplatedParent
。因此,与标准
Binding
s类似,只能对单个属性进行设置。从MSDN上的页面:

您可以在template中使用
TemplateBinding
绑定到应用模板的控件上的值

从MSDN上的页面:

<代码>模板绑定只有一个可设置的属性


这意味着我们不能为它提供多个属性名称,因此它只能使用单个属性。不幸的是,没有一种方便的
SuperTemplateBinding
可以影响所有属性,但即使有,我也无法想象任何简单的方法来映射哪些属性应该绑定到哪些模板属性。因此,答案是“不,没有从模板化父控件继承属性值的快捷方式”。

不,没有。。。您必须设置要使用的每个属性。真的吗-(好吧,我猜否定的答案也是答案。也没有解决办法吗?虽然你创建了自己的TextBox类,但它与普通情况没有区别。你覆盖默认模板,为了从父属性中获取任何值,你需要做TemplateBinding。你能不能详细说明一下?我不完全理解了解此代码所在的位置和/或可以编辑的位置。因为在文本框样式中没有名为
Border
的属性可以编辑(只有
BorderThickness
BorderBrush
)。在模板中,您应该使用具有特殊名称“PART\u ContentHost”的
ScrollViewer
。请参阅我的编辑。
TemplateBinding
RelativeSource.TemplatedParent
不相同。首先在编译时解析,然后在运行时解析。
    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
        <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
    </Border>
 <Style TargetType="{x:Type my:MySpecialTextBox}">
     <Setter Property="Template">
         <Setter.Value>
             <ControlTemplate TargetType="{x:Type my:MySpecialTextBox}">
                 <StackPanel>
                     <Rectangle Width="100" Height="3" Fill="Pink" />
                     <!-- My own fancy lay-out -->
                     <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                         <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                     </Border>
                 </StackPanel>
             </ControlTemplate>
         </Setter.Value>
     </Setter>
 </Style>