Binding Mutiple Child Window Controls to Window Dependency Property [modified]
-
Given a dependecy property "Val":
public partial class Window3 : Window { public Window3() { InitializeComponent(); } public static readonly DependencyProperty ValProperty = DependencyProperty.Register("Val", typeof(float), typeof(Window3), new FrameworkPropertyMetadata((float)50.0)); public float Val { get { return (float)GetValue(ValProperty); } set { SetValue(ValProperty, value); } } }
How can I bind multiple child controls to it? Following does not work:<Window x:Class="ZoomTest.Window3" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window3" Height="300" Width="300"> <StackPanel> <ProgressBar x:Name="Progress" Value="{Binding Path=Val, ElementName=Window3, Mode=Default}"/> <Slider x:Name="MySlider" Value="{Binding Path=Val, ElementName=Window3, Mode=Default}" Minimum="{Binding Path=Minimum, ElementName=Progress, Mode=Default}" Maximum="{Binding Path=Maximum, ElementName=Progress, Mode=Default}"/> </StackPanel> </Window>
Sincerely, -Ron
modified on Thursday, April 17, 2008 6:04 PM
-
Given a dependecy property "Val":
public partial class Window3 : Window { public Window3() { InitializeComponent(); } public static readonly DependencyProperty ValProperty = DependencyProperty.Register("Val", typeof(float), typeof(Window3), new FrameworkPropertyMetadata((float)50.0)); public float Val { get { return (float)GetValue(ValProperty); } set { SetValue(ValProperty, value); } } }
How can I bind multiple child controls to it? Following does not work:<Window x:Class="ZoomTest.Window3" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window3" Height="300" Width="300"> <StackPanel> <ProgressBar x:Name="Progress" Value="{Binding Path=Val, ElementName=Window3, Mode=Default}"/> <Slider x:Name="MySlider" Value="{Binding Path=Val, ElementName=Window3, Mode=Default}" Minimum="{Binding Path=Minimum, ElementName=Progress, Mode=Default}" Maximum="{Binding Path=Maximum, ElementName=Progress, Mode=Default}"/> </StackPanel> </Window>
Sincerely, -Ron
modified on Thursday, April 17, 2008 6:04 PM
Ok. This works.
<Window x:Class="ZoomTest.Window3" x:Name="Me" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window3" Height="300" Width="300"> <StackPanel> <ProgressBar x:Name="Progress" Value="{Binding Path=Val, ElementName=Me, Mode=Default}"/> <Slider x:Name="MySlider" Value="{Binding Path=Val, ElementName=Me, Mode=Default}" Minimum="{Binding Path=Minimum, ElementName=Progress, Mode=Default}" Maximum="{Binding Path=Maximum, ElementName=Progress, Mode=Default}"/> </StackPanel> </Window>
But I hate Me. It reminds me of VB. Ideally this would be more concise but doesn't work:<Window x:Class="ZoomTest.Window3" x:Name="Me" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window3" Height="300" Width="300"> <StackPanel> <ProgressBar x:Name="Progress" Value="{Binding Path=Val}"/> <Slider x:Name="MySlider" Value="{Binding Path=Val}" Minimum="{Binding Path=Minimum, ElementName=Progress}" Maximum="{Binding Path=Maximum, ElementName=Progress}"/> </StackPanel> </Window>
Is there any way to set the default data context to the window for all of it's children?Sincerely, -Ron