Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some little question. I have 2 view models and and 2 corresponding views. On the first view (user control) i have checkbox:

C#
<UserControl x:Class="ZemaxWorks.UI.Views.DetectorViewer"
         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:controls="clr-namespace:ZemaxWorks.UI.Controls"/>
...
        <StackPanel Grid.Column="1" Grid.Row="1" HorizontalAlignment="Right" Orientation="Horizontal">
        <CheckBox x:Name="Scales" Style="{StaticResource CheckBoxBase}" Content="{x:Static res:Resource.DETECTOR_VIEWER_EQUAL_SCALES}"
                  IsChecked="{Binding ElementName=Scales, Path=EqualScales}"
                  />
    </StackPanel>
</Grid>


Then i create a dependensy property and bind it to "IsChecked" property of CheckBox:

C#
public partial class DetectorViewer : UserControl
{
    public bool EqualScales
    {
        get { return (bool)GetValue(EqualScalesProperty); }
        set { SetValue(EqualScalesProperty, value); }
    }

    // Using a DependencyProperty as the backing store for EqualScales.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty EqualScalesProperty =
        DependencyProperty.Register(nameof(EqualScales), typeof(bool), typeof(DetectorViewer));

    public DetectorViewer()
    {
        InitializeComponent();
    }
}


On the main window i have a few tabs, where need to show my user control with checkbox. I describe it by template:

C#
<views:ViewBase x:Class="ZemaxWorks.UI.Views.BeamSizeShowDataDialog"
             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"/>
...
<views:DetectorViewer Grid.Row="1" DataContext="{Binding}" EqualScales="{Binding DataContext.IsAllEqualScales, Mode=TwoWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"/>
...
</views:ViewBase>


In the view model for this view i create property:

C#
public bool IsAllEqualScales
        {
            get
            {
                return allEqualScales;
            }
            set
            {
                SetProperty(ref allEqualScales, value);
            }
        }


But binding don't work. I need to bind dependency property from user control to property of BeamSizeShowDataDialog view model with TwoWay mode. How need to correctly do it ?? Sorry for some errors in my message. Thanks all.

What I have tried:

I tried to change code:

<usercontrol x:class="ZemaxWorks.UI.Views.DetectorViewer" xmlns:x="#unknown">
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:controls="clr-namespace:ZemaxWorks.UI.Controls"/>
...
<stackpanel grid.column="1" grid.row="1" horizontalalignment="Right" orientation="Horizontal">
<checkbox x:name="Scales" style="{StaticResource CheckBoxBase}" content="{x:Static res:Resource.DETECTOR_VIEWER_EQUAL_SCALES}">
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=EqualScales}"
/>


but it isn't work too.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900