Hi! Why does not working bool dependency property of checkbox in UserControl. I want to use it with converter to hide the button control.
<UserControl
x:Class="MyLib.Legend"
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"
mc:Ignorable="d" Height="28" Width="80">
<Grid Margin="0,0,0,253">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"></ColumnDefinition>
<ColumnDefinition Width="30"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Margin="0,0,0,-25" Content="График 1" />
<CheckBox IsChecked="{Binding ElementName=leg2, Path=IsChecked}" Grid.Column="1" HorizontalAlignment="Left" Height="15" Margin="7,7,0,-22" VerticalAlignment="Top" Width="18"/>
</Grid>
</UserControl>
Legend.xaml.cs
public partial class Legend :UserControl
{
public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(Legend), null);
public bool IsChecked
{
get { return (bool)GetValue(IsCheckedProperty); }
set { SetValue(IsCheckedProperty, value); }
}
public event RoutedEventHandler IsChecked
{
add
{
base.AddHandler(Legend.MyClickEvent, value);
}
remove
{
base.RemoveHandler(Legend.MyClickEvent, value);
}
}
public Legend()
{
InitializeComponent();
}
}
MainWindow.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:oxy="http://oxyplot.codeplex.com"
xmlns:Custom="http://infragistics.com/DataPresenter"
xmlns:local="clr-namespace:WpfExamples"
xmlns:MyLib="clr-namespace:MyLib;assembly=MyLib"
xmlns:MyLibrary="clr-namespace:MyLibrary;assembly=MyLibrary"
x:Class="WpfExamples.MainWindow"
Title="OxyPlot WPF/XAML examples" Height="628.633" Width="1024.91">
<Grid Margin="0,0,25,48" >
<Grid.Resources>
<local:Title_View2 x:Key="Legend2_View"/>
</Grid.Resources>
<MyLib:Legend Name="leg2" HorizontalAlignment="Left" Margin="573,267,0,0" VerticalAlignment="Top"/>
<Button
Visibility="{Binding IsChecked, Converter={StaticResource Legend2_View}, ElementName=leg2}"
Content="Button"
HorizontalAlignment="Left" Margin="591,317,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
MainWindow.cs
...
public class Title_View2 : IValueConverter
{
public object Convert(object value, Type targetTipe, object parameter, CultureInfo culture)
{
if (value is bool)
return ((bool)value) ? Visibility.Visible : Visibility.Collapsed;
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}