简体   繁体   English

wpf绑定到另一个控件属性

[英]wpf binding to another control property

so i have this usercontrol : 所以我有这个用户控件:

<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>

and as you can see im trying to bind the MainWindow.availableAntennas[0] property to the MenuItem.IsEnabled property 如您所见,我正在尝试将MainWindow.availableAntennas [0]属性绑定到MenuItem.IsEnabled属性

this is how i declared the array on MainWindow class: 这就是我在MainWindow类上声明数组的方式:

public  bool[]      availableAntennas   = new bool[9];

my problem is that the IsEnabled property is always true and yes i double checked the availeableAntennas[0] is false, so what am i doing wrong here? 我的问题是IsEnabled属性始终为true,是的,我再次检查了availeableAntennas [0]为false,那么我在这里做错了什么?

You should use "RelativeSource" of the binding expression. 您应该使用绑定表达式的"RelativeSource"

Cheers 干杯

To me, it looks like it could be a few things or all of them: 在我看来,可能是其中的一些或全部:

  • You can't bind to a field it must be a property - public bool[] availableAntennas = new bool[9] , 您无法绑定到必须为属性的字段-public bool [] availableAntennas = new bool [9]
  • A typo - availeableAntennas vs. availableAntennas , 拼写错误- 可用天线与可用天线
  • The datacontext must be set(it's not apparent from your posting), 必须设置数据上下文(从您的发布中看不出来),
  • The class you are bound to must implement INotifyPropertyChanged. 您绑定的类必须实现INotifyPropertyChanged。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM