Click here to Skip to main content
13,142,182 members (57,138 online)
Rate this:
 
Please Sign up or sign in to 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.

 <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();
        }
    }
Posted 19-Feb-13 6:00am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.170915.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100