如何在WPF页面中将网格的数据上下文设置为另一个WPF窗口 [英] How to set the data context of a grid in a WPF page to an other WPF window

查看:111
本文介绍了如何在WPF页面中将网格的数据上下文设置为另一个WPF窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF的新手。我有一个WPF窗口,并且在同一项目中有一个WPF页面。该页面包含要加载到窗口中的键盘布局。该窗口有一个框架,其源设置为页面,现在页面中的所有按钮都是WPF自定义控件,这些控件具有两个依赖项属性,例如IsCapsOn和IsShiftOn。这些依赖项属性在更改时会更改按钮上的文本( a变为 A,指示按下了大写字母)。
上面的依赖项属性通过名称IsCapsPressed和IsShiftPressed绑定到主窗口的依赖项属性。当用户在主窗口上按下Caps键时,IsCapsPressed属性在后端发生更改,因此IsCapsOn也必须由于绑定而更改。为此,我必须将页面中按钮的数据上下文设置为mainwindow,因为在其中定义了依赖项属性。以下是代码片段:

I am a newbie to WPF.I have a WPF window and in the same project I have a WPF page.The page contains the keyboard layout to be loaded into the window.The window has a frame whose source is set to the page.Now all the buttons in page are WPF custom controls.These controls have two dependency properties like IsCapsOn and IsShiftOn. These dependency properties when changed changes the text on buttons("a" becomes "A" indicating that caps is pressed). The above dependency properties are binded to dependency properties of mainwindow by name IsCapsPressed and IsShiftPressed. When user presses caps key on mainwindow, IsCapsPressed property changes in the backend and as a result IsCapsOn also has to change due to binding.For this i have to set the data context of buttons in page to mainwindow because dependency properties are defined in it.Following are code snipets:

 //page
<Page x:Class="Philips.PmsUS.VirtualKeyboard.Resources.Layouts.USKeyboardPage"
      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:kb="clr-namespace:Philips.PmsUS.KeyboardButton;assembly=Philips.PmsUS.KeyboardButton"
      mc:Ignorable="d"
      x:Name="Layout"
      Loaded="Page_Loaded"      
      Title="USKeyboardPage">    
    <Grid x:Name="MainGrid"
          Width="auto"
          Height="auto"
          Focusable="True">
    <!...Row and column definitions are defined...>
     <kb:KeyboardButton Name="Button_Q"
                               Width="auto"
                               Height="auto"
                               Grid.Column="3"                               
                               IsCapsOn="{Binding Path=IsCapsPressed}"
                               IsShiftOn="{Binding Path=IsShiftPressed}"
                               Focusable="True"
                               Style="{StaticResource KeyboardButtonStyle}"
                               Click="NonModifierClick"
                               DataContext="{Binding ElementName=Keyboardwnd}"></kb:KeyboardButton>        
    </Grid>
<page>

//window.xaml
<Window x:Class="Philips.PmsUS.VirtualKeyboard.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"               
        Title="KeyboardWindow"
        x:Name="Keyboardwnd"        
        Height="581"
        Width="1392"
        Topmost="False"
        Loaded="Window_Loaded"
        FocusManager.IsFocusScope="True"
        WindowStartupLocation="Manual"
        Background="{DynamicResource KeyboardBackgroundBrush}">   
    <Grid x:Name="LayoutRoot">
        <Frame x:Name="LayoutHolder"
               Source="Resources\Layouts\USKeyboardPage.xaml"
               ></Frame>            
    </Grid>
  </Window>

//mainwindow.xaml.cs
#region dependency properties
        //the below dependency properties are used for modifier keys
        public static readonly DependencyProperty IsCapsPressedProperty = DependencyProperty.Register("IsCapsPressed", typeof(bool), typeof(MainWindow), new FrameworkPropertyMetadata(new PropertyChangedCallback(SetCapsState)));
        public static readonly DependencyProperty IsShiftPressedProperty = DependencyProperty.Register("IsShiftPressed", typeof(bool), typeof(MainWindow), new FrameworkPropertyMetadata(new PropertyChangedCallback(SetShiftState)));
        public static readonly DependencyProperty IsGlobePressedProperty = DependencyProperty.Register("IsGlobePressed", typeof(bool), typeof(MainWindow), new FrameworkPropertyMetadata(new PropertyChangedCallback(SetGlobeState)));
        #endregion

但以上数据上下文绑定无效,请提供帮助。

But the above Data context binding is not working.Please help.

推荐答案

尝试设置Window的DataContext。在窗口的构造函数中,在InitializeComponents()之后执行 DataContext = this;

Try setting the DataContext of the Window. In constructor of your window do DataContext = this; after InitializeComponents().

谢谢

这篇关于如何在WPF页面中将网格的数据上下文设置为另一个WPF窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆