Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi! Why does not working bool dependency property of checkbox in UserControl. I want to use it with converter to hide the button control.

XML
 <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>


C#
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();
        }       
    }


XML
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>



C#
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();
        }
    }
Posted

1 solution

Hi. i think because of your Dependency Property Name.
IsChecked has defined in Default and compiler encounter with ambiguity. you try try this:
Change your Dependency Property Name from IsChecked to anything. i hope this solve your problem.
goodluck
 
Share this answer
 
v2

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