C# wpf绑定到另一个控件属性
我有一个用户控件:C# wpf绑定到另一个控件属性,c#,wpf,binding,C#,Wpf,Binding,我有一个用户控件: <UserControl x:Class="Client.SpectrumSpace" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlf
<UserControl x:Class="Client.SpectrumSpace"
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"
xmlns:my="clr-namespace:Client"
mc:Ignorable="d" d:DesignHeight="150" d:DesignWidth="350">
<Canvas>
<Rectangle Width="350" Height="150" Fill="Transparent" Stroke="White" StrokeThickness="1">
<Rectangle.ContextMenu>
<ContextMenu Name="contextMenu">
<MenuItem Name="ctxItem1" Header="AntennaName"
IsEnabled="{Binding MainWindow.availeableAntennas[0]}"/>
</ContextMenu>
</Rectangle.ContextMenu>
</Rectangle>
</Canvas>
</UserControl>
我的问题是IsEnabled属性始终为true,是的,我仔细检查了AvailableAttennas[0]是否为false,那么我在这里做错了什么?您应该使用绑定表达式的“RelativeSource”
干杯对我来说,可能是几件事,也可能是全部:
- 您不能绑定到字段,它必须是属性-public bool[]availableAntennas=new bool[9]
- 一个打字错误-可用的天线vs.可用的天线
- 必须设置datacontext(这在您的帖子中并不明显)
- 绑定到的类必须实现INotifyPropertyChanged
public bool[] availableAntennas = new bool[9];