【发布时间】:2015-05-06 15:28:45
【问题描述】:
我是 XAML 数据绑定的新手,我陷入了这种情况。我正在使用 Mahapps MetroWindow。
假设我有一个名为 usrctrl_Camera_Control 的用户控件。我有一个简单的按钮。 C#代码如下。
namespace TA141501005
{
public partial class usrctrl_Camera_Control : UserControl
{
public usrctrl_Camera_Control()
{
this.DataContext = this;
InitializeComponent();
}
}
XAML 如下所示
<UserControl
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:Custom="http://metro.mahapps.com/winfx/xaml/controls" xmlns:local="clr-namespace:TA141501005" x:Class="TA141501005.usrctrl_Camera_Control"
mc:Ignorable="d"
d:DesignHeight="768" d:DesignWidth="1366" Background="#FF2B2B2B" Height="738" Width="1336">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Icons.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Button x:Name="btn_Test" Content="Button" Grid.Column="1" HorizontalAlignment="Left" Margin="472,59,0,0" VerticalAlignment="Top" Width="75" Grid.RowSpan="2" IsEnabled="{Binding Path=IsNyetEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}}" />
</Grid>
MainWindow 的 C# 代码如下。
namespace TA141501005
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : MetroWindow
{
usrctrl_Camera_Control usrctrl_camera_control;
public bool IsNyetEnabled { get; set; }
public MainWindow()
{
IsNyetEnabled = false;
this.DataContext = this;
InitializeComponent();
usrctrl_camera_control = new usrctrl_Camera_Control();
}
}
}
MainWindow 的 XAML 代码如下所示。
<Controls:MetroWindow x:Class="TA141501005.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
Title="Macromium System Control V1.0ES - TA141501005 [Engineering Sample]" Height="768" Width="1366" Background="#FF2B2B2B" ScrollViewer.VerticalScrollBarVisibility="Disabled" ResizeMode="NoResize" WindowStyle="ThreeDBorderWindow" WindowStartupLocation="CenterScreen" IsMinButtonEnabled="False" IsWindowDraggable="False" ShowMaxRestoreButton="False" ShowMinButton="False" ShowSystemMenuOnRightClick="False" IconOverlayBehavior="Flyouts">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Icons.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Button x:Name="btn_test" Content="Button" HorizontalAlignment="Left" Margin="589,590,0,0" VerticalAlignment="Top" Width="75" IsEnabled="{Binding IsNyetEnabled}"/>
</Grid>
我想将 usrctrl_Camera_Control 中的 btn_test 和 MainWindow 中的 btn_test 中的属性 IsEnabled 绑定到 MainWindow 中的 IsNyetEnabled。在 MainWindow 中执行 InitializeComponent() 之前,我将 IsNyetEnabled 设置为 false。
MainWindow 中的 btn_test.IsEnabled 与 MainWindow 中的 IsNyetEnabled 之间的绑定完美无缺。 MainWindow 中的 btn_test 不再启用。 (我知道,如果有任何更改,我需要实现 INotifyPropertyChanged 来通知订阅者,但为了简单起见,暂时保持原样。
但是,usrctrl_Camera_Control 中的 btn_test.IsEnabled 与 MainWindow 中的 IsNyetEnabled 绑定失败。我使用了 Visual Studio“创建数据绑定”向导,但编译时总是返回错误。
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='TA141501005.MainWindow', AncestorLevel='1''. BindingExpression:Path=IsNyetEnabled; DataItem=null; target element is 'Button' (Name='btn_Test'); target property is 'IsEnabled' (type 'Boolean')
你有什么建议吗?我已经尝试了一整天没有运气。 有没有办法在不删除 this.Datacontext = this 的情况下访问父数据上下文? 期待您的建议和解释。 非常感谢。
编辑。 我通过 Flyout 显示我的 UserControl。
Window parentWindow = Window.GetWindow(this);
object obj = parentWindow.FindName("mainFlyout");
Flyout flyout = (Flyout) obj;
flyout.Content = new SomeFlyOutUserControl();
flyout.IsOpen = !flyout.IsOpen;
【问题讨论】:
-
您是否使用
Frame在MainWindow中显示UserControl?请在问题中添加那段代码。 -
@AnandMurali 我正在使用浮出控件来显示我的 UserControl。窗口 parentWindow = Window.GetWindow(this);对象 obj = parentWindow.FindName("mainFlyout"); Flyout flyout = (Flyout) obj; flyout.Content = new SomeFlyOutUserControl(); flyout.IsOpen = !flyout.IsOpen;