Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ToggleSwitch add OffContent and OnContent #602

Merged
merged 3 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions src/Wpf.Ui.Gallery/Views/Pages/BasicInput/ToggleSwitchPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,31 @@
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ui:ToggleSwitch
Grid.Column="0"
Content="WPF UI toggle switch"
IsEnabled="{Binding ViewModel.IsToggleSwitchEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ToggleSwitchPage}, Mode=OneWay}" />
<CheckBox
Grid.Column="1"
Command="{Binding ViewModel.ToggleSwitchCheckboxCheckedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ToggleSwitchPage}, Mode=OneWay}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Mode=OneWay}"
Content="Disable switch" />

<Grid Grid.Column="0" Margin="42">
<controls:GalleryControlPresenter
Grid.Row="0"
Margin="0"
CodeText="&lt;ui:ToggleSwitch OffContent=&quot;Off&quot; OnContent=&quot;On&quot; /&gt;"
HeaderText="WPF UI toggle switch.">
<controls:GalleryControlPresenter.Content>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ui:ToggleSwitch
Grid.Column="0" OffContent="Off" OnContent="On"
IsEnabled="{Binding ViewModel.IsToggleSwitchEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ToggleSwitchPage}, Mode=OneWay}" />
<CheckBox
Grid.Column="1"
Command="{Binding ViewModel.ToggleSwitchCheckboxCheckedCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ToggleSwitchPage}, Mode=OneWay}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Mode=OneWay}"
Content="Disable switch" />
</Grid>
</controls:GalleryControlPresenter.Content>
</controls:GalleryControlPresenter>
</Grid>
</Grid>
</controls:ControlExample>
</Grid>
Expand Down
46 changes: 42 additions & 4 deletions src/Wpf.Ui/Controls/ToggleSwitch/ToggleSwitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,50 @@
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

// ReSharper disable once CheckNamespace
using System.ComponentModel;
using System.Drawing;
using System.Windows;

namespace Wpf.Ui.Controls;

/// <summary>
/// Use <see cref="ToggleSwitch"/> to present users with two mutally exclusive options (like on/off).
/// </summary>
//[ToolboxItem(true)]
//[ToolboxBitmap(typeof(ToggleSwitch), "ToggleSwitch.bmp")]
public class ToggleSwitch : System.Windows.Controls.Primitives.ToggleButton { }
[ToolboxItem(true)]
[ToolboxBitmap(typeof(ToggleSwitch), "ToggleSwitch.bmp")]

Check failure on line 16 in src/Wpf.Ui/Controls/ToggleSwitch/ToggleSwitch.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ToolboxBitmapAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 16 in src/Wpf.Ui/Controls/ToggleSwitch/ToggleSwitch.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ToolboxBitmap' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 16 in src/Wpf.Ui/Controls/ToggleSwitch/ToggleSwitch.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ToolboxBitmapAttribute' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 16 in src/Wpf.Ui/Controls/ToggleSwitch/ToggleSwitch.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'ToolboxBitmap' could not be found (are you missing a using directive or an assembly reference?)
public class ToggleSwitch : System.Windows.Controls.Primitives.ToggleButton
{
/// <summary>
/// Property for <see cref="OffContent"/>.
/// </summary>
public static readonly DependencyProperty OffContentProperty = DependencyProperty.Register(
"OffContent", typeof(object), typeof(ToggleSwitch), new PropertyMetadata(null));

/// <summary>
/// Property for <see cref="OnContent"/>.
/// </summary>
public static readonly DependencyProperty OnContentProperty = DependencyProperty.Register(
"OnContent", typeof(object), typeof(ToggleSwitch), new PropertyMetadata(null));

/// <summary>
/// Provides the object content that should be displayed when this
/// <see cref="ToggleSwitch" /> has state of "Off".
/// </summary>
[Bindable(true)]
public object OffContent
{
get => GetValue(OffContentProperty);
set => SetValue(OffContentProperty, value);
}

/// <summary>
/// Provides the object content that should be displayed when this
/// <see cref="ToggleSwitch" /> has state of "On".
/// </summary>
[Bindable(true)]
public object OnContent
{
get => GetValue(OnContentProperty);
set => SetValue(OnContentProperty, value);
}
}
52 changes: 48 additions & 4 deletions src/Wpf.Ui/Controls/ToggleSwitch/ToggleSwitch.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,24 @@
TextElement.Foreground="{TemplateBinding Foreground}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Content" Value="{x:Null}">
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Content" Value="{x:Null}" />
<Condition Property="OnContent" Value="{x:Null}" />
<Condition Property="OffContent" Value="{x:Null}" />
</MultiTrigger.Conditions>
<Setter TargetName="ContentPresenter" Property="Margin" Value="0" />
</Trigger>
<Trigger Property="Content" Value="">
</MultiTrigger>

<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Content" Value="" />
<Condition Property="OnContent" Value="" />
<Condition Property="OffContent" Value="" />
</MultiTrigger.Conditions>
<Setter TargetName="ContentPresenter" Property="Margin" Value="0" />
</Trigger>
</MultiTrigger>

<Trigger Property="IsChecked" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
Expand Down Expand Up @@ -219,6 +231,38 @@
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Content" Value="{x:Null}" />
<Condition Property="IsChecked" Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="ContentPresenter" Property="Content" Value="{Binding OnContent, RelativeSource={RelativeSource TemplatedParent}}" />
</MultiTrigger>

<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Content" Value="{x:Null}" />
<Condition Property="IsChecked" Value="False" />
</MultiTrigger.Conditions>
<Setter TargetName="ContentPresenter" Property="Content" Value="{Binding OffContent, RelativeSource={RelativeSource TemplatedParent}}" />
</MultiTrigger>

<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Content" Value="" />
<Condition Property="IsChecked" Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="ContentPresenter" Property="Content" Value="{Binding OnContent, RelativeSource={RelativeSource TemplatedParent}}" />
</MultiTrigger>

<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Content" Value="" />
<Condition Property="IsChecked" Value="False" />
</MultiTrigger.Conditions>
<Setter TargetName="ContentPresenter" Property="Content" Value="{Binding OffContent, RelativeSource={RelativeSource TemplatedParent}}" />
</MultiTrigger>

<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
Expand Down
Loading