I have some little question. I have 2 view models and and 2 corresponding views. On the first view (user control) i have checkbox:
<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:
public partial class DetectorViewer : UserControl
{
public bool EqualScales
{
get { return (bool)GetValue(EqualScalesProperty); }
set { SetValue(EqualScalesProperty, value); }
}
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:
<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:
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.