更新时间:2022-05-02 02:34:24
如果您查看输出窗口,您应该会看到绑定异常.
if you look at your output window you should see the binding exception.
您遇到的问题如下:在您的用户控件中,您会将标签绑定到用户控件的 DP ProtocolNumber 而不是 DataContext
,因此您必须将例如元素名称添加到绑定.
The problem you have is the following: within your usercontrol you will bind the label to the DP ProtocolNumber of your usercontrol and not the DataContext
, so you have to add for example the element name to the binding.
<UserControl Name="MainOptionsPanel"
x:Class="ExperienceMainControls.MainControls"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="uc"
>
<Label Height="Auto" Name="numberLabel">Protocol:</Label>
<Label Content="{Binding Path=ProtocolNumber, ElementName=uc}" Name="protocolNumberLabel"/>
(...)
</UserControl>
为了解决一些问题,如果您更改 MainWindow 中的绑定,您的用户控件也可以工作.但是你必须使用RelativeSource绑定到MainWindow的DataContext.
to clear some things up, your usercontrol also works if you change the binding in your MainWindow. but you have to bind to the DataContext of the MainWindow with RelativeSource.
<expControl:MainControls ProtocolNumber="{Binding Path=Number, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />