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.