How to get Dependency-Object and -Property for Usercontrol in VB and WPF?

Hans Hissen 21 Reputation points
2021-04-09T09:46:32.26+00:00

With VB and WPF I build a Usercontrol "ToggleSwitch". Want to bind it's IsChecked-Property to another signal as I did with Checkbox.
How to get Dependency-Object and Dependency-Property for that in VB and WPF?

Thanks

Developer technologies | VB
0 comments No comments
{count} votes

Answer accepted by question author
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2021-04-09T11:22:47.763+00:00

    Hi,
    you can register static (shared) property like in following demo:

    XAML UserControl:

    <UserControl x:Class="Window088ToggleSwitch"  
                 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:local="clr-namespace:WpfControlLibrary1"  
                 mc:Ignorable="d"   
                 d:DesignHeight="450" d:DesignWidth="800">  
      <StackPanel x:Name="stp">  
        <CheckBox Content="Binded IsChecked" IsChecked="{Binding IsChecked}" IsEnabled="False"/>  
      </StackPanel>  
    </UserControl>  
    

    CodeBehind UserControl:

    Public Class Window088ToggleSwitch  
      
      Public Sub New()  
      
        ' This call is required by the designer.  
        InitializeComponent()  
      
        ' Add any initialization after the InitializeComponent() call.  
        stp.DataContext = Me  
      End Sub  
      
      Public Shared ReadOnly IsCheckedProperty As DependencyProperty =  
              DependencyProperty.RegisterAttached("IsChecked", GetType(Boolean),  
                GetType(Window088ToggleSwitch), New PropertyMetadata(Nothing))  
      
      Public Shared Function GetIsChecked(obj As DependencyObject) As Boolean  
        Return CType(obj.GetValue(IsCheckedProperty), Boolean)  
      End Function  
      
      Public Shared Sub SetIsChecked(obj As DependencyObject, value As Boolean)  
        obj.SetValue(IsCheckedProperty, value)  
      End Sub  
      
    End Class  
    

    XAML MainWindow:

    <Window x:Class="Window088"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:WpfApp1"  
            xmlns:controls="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"  
            mc:Ignorable="d"  
            Title="MainWindow" Height="450" Width="800">  
      <StackPanel>  
        <CheckBox x:Name="cb" Content="Main CheckBox"/>  
        <controls:Window088ToggleSwitch IsChecked="{Binding ElementName=cb, Path=IsChecked}" />  
      </StackPanel>  
    </Window>  
    

    Result:

    86208-x.gif

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Hans Hissen 21 Reputation points
    2021-04-11T10:35:31.057+00:00

    Peter Fleischer thank you for the Demo. Got it run on my PC.
    Thanks a lot!

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.