question

HansHissen-9260 avatar image
0 Votes"
HansHissen-9260 asked HansHissen-9260 answered

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

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

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered

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



x.gif (58.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

HansHissen-9260 avatar image
0 Votes"
HansHissen-9260 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.