C# WPF-ContentPresenter未绑定到UserControl内容

C# WPF-ContentPresenter未绑定到UserControl内容,c#,wpf,xaml,user-controls,blend,C#,Wpf,Xaml,User Controls,Blend,好的,我对WPF比较陌生,我的usercontrol有点问题,它包含一个自定义按钮 在我尝试将自定义按钮的内容绑定到usercontrol内容之前,一切都很正常 用户控制代码: 我在主窗口中以以下方式声明usercontrol: <local:GamepadButton Height="60" Width="60" Background="#DAF0FC" BorderBrush="#2c3e50" Content="Button"/> 结果如下: 此外,我现在注意到,

好的,我对WPF比较陌生,我的usercontrol有点问题,它包含一个自定义按钮

在我尝试将自定义按钮的内容绑定到usercontrol内容之前,一切都很正常

用户控制代码:

我在主窗口中以以下方式声明usercontrol:

<local:GamepadButton Height="60" Width="60" Background="#DAF0FC" BorderBrush="#2c3e50" Content="Button"/>

结果如下:

此外,我现在注意到,出于某种原因,这也会破坏边界笔刷绑定,但不会破坏背景绑定


如果我没有为Usercontrol声明任何内容属性并直接设置按钮内容,那么代码就可以工作:

用户控制:

主窗口:

这给了我想要的按钮样式:


所以我的问题是


如何使按钮的ContentPresenter绑定到为usercontrol声明的内容?

您面临的问题是无法从该内容内部绑定到内容。用户控件内的按钮是控件的当前内容。在输出窗口中,您可以看到以下行

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='OP.GamepadButton', AncestorLevel='1''. BindingExpression:Path=Tag; DataItem=null; target element is 'Button' (Name=''); target property is 'Content' (type 'Object')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='OP.GamepadButton', AncestorLevel='1''. BindingExpression:Path=Background; DataItem=null; target element is 'Button' (Name=''); target property is 'Background' (type 'Brush')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='OP.GamepadButton', AncestorLevel='1''. BindingExpression:Path=BorderBrush; DataItem=null; target element is 'Button' (Name=''); target property is 'BorderBrush' (type 'Brush')
如果将其更改为其他属性(如标记),则样式将正确应用


我认为您可以只使用style来解决您的任务,但是如果您确实需要新的UserControl,那么您可以直接从button继承它,而不是UserControl。

谢谢!很好地工作了,现在我觉得自己没有弄明白这一点很愚蠢。接下来的问题是,我很确定我需要它成为一个用户控件(由于未来关于如何使用它的计划),但如果GamepadButton直接从button继承而不是用户控件,GamepadButton:UserControl和GamepadButton:Button在如何使用上有什么区别?在使用上没有区别,但在功能上有一些细微的区别。GamepadButton:按钮可以直接用于从xaml生成嵌套内容,这有时非常有用。另一方面,GamepadButton:UserControl对于同一个函数需要一些dependencProperty,因为正如您之前看到的,您无法为UserControl指定内容,因为它已经预定义了一个内容。
<Button Content="Button" 
    Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:GamepadButton}}, Path=Background}" 
    BorderBrush="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:GamepadButton}}, Path=BorderBrush}" 
    Grid.Row="1" Style="{DynamicResource GamepadButtonStyle}" />
<local:GamepadButton Height="60" Width="60" Background="#DAF0FC" BorderBrush="#2c3e50" />
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='OP.GamepadButton', AncestorLevel='1''. BindingExpression:Path=Tag; DataItem=null; target element is 'Button' (Name=''); target property is 'Content' (type 'Object')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='OP.GamepadButton', AncestorLevel='1''. BindingExpression:Path=Background; DataItem=null; target element is 'Button' (Name=''); target property is 'Background' (type 'Brush')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='OP.GamepadButton', AncestorLevel='1''. BindingExpression:Path=BorderBrush; DataItem=null; target element is 'Button' (Name=''); target property is 'BorderBrush' (type 'Brush')