Gaz*_*yer 2 c# wpf xaml binding contentcontrol
我有一个自定义ContentControl,它具有固定的XAML布局,如UserControl(不是通常应用的通用模板).
以前这种布局没有额外的标记,所以它实际上是:
<ContentControl x:Class="MyControls.CustomViewControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
</ContentControl>
Run Code Online (Sandbox Code Playgroud)
这很好.
我现在想在内容周围添加边框,因此我将XAML更改为:
<ContentControl x:Class="MyControls.CustomViewControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<ContentControl.Template>
<ControlTemplate>
<Border BorderThickness="5" BorderBrush="LightGreen">
<ContentPresenter />
</Border>
</ControlTemplate>
</ContentControl.Template>
</ContentControl>
Run Code Online (Sandbox Code Playgroud)
这显示了边框,但没有内容.
我尝试为ContentPresenter提供显式绑定:
<ContentPresenter Content="{Binding Path=Content, RelativeSource={RelativeSource Self}}"/>
Run Code Online (Sandbox Code Playgroud)
但这没有任何区别.
设置显式Content确实有效:
<ContentPresenter Content="TEST" />
Run Code Online (Sandbox Code Playgroud)
任何人都知道为什么内容绑定不起作用?我想我可以回到通常的通用模板,但如果我可以像UserControl一样直接做它会更容易.
为控件模板添加TargetType
<ContentControl.Template>
<ControlTemplate TargetType="Button">
<Border BorderThickness="5" BorderBrush="LightGreen">
<ContentPresenter />
</Border>
</ControlTemplate>
</ContentControl.Template>
Run Code Online (Sandbox Code Playgroud)