Skip to content

Commit

Permalink
Add attached
Browse files Browse the repository at this point in the history
  • Loading branch information
davideCappe committed May 10, 2024
1 parent 39b8a00 commit 4df4258
Show file tree
Hide file tree
Showing 11 changed files with 374 additions and 2 deletions.
9 changes: 8 additions & 1 deletion CodeSculpt.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{312353EA-EB37-49C5-A6AD-4B50CF325C8A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeSculpt.Wpf", "src\CodeSculpt.Wpf\CodeSculpt.Wpf.csproj", "{E2EE3394-676A-48C9-9088-D2F081C61170}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeSculpt.Wpf", "src\CodeSculpt.Wpf\CodeSculpt.Wpf.csproj", "{E2EE3394-676A-48C9-9088-D2F081C61170}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeSculpt.Wpf.Sample", "samples\CodeSculpt.Wpf.Sample\CodeSculpt.Wpf.Sample.csproj", "{FCF6A2CF-3735-406F-B516-FD3ABF749409}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -34,12 +36,17 @@ Global
{E2EE3394-676A-48C9-9088-D2F081C61170}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E2EE3394-676A-48C9-9088-D2F081C61170}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E2EE3394-676A-48C9-9088-D2F081C61170}.Release|Any CPU.Build.0 = Release|Any CPU
{FCF6A2CF-3735-406F-B516-FD3ABF749409}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FCF6A2CF-3735-406F-B516-FD3ABF749409}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FCF6A2CF-3735-406F-B516-FD3ABF749409}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FCF6A2CF-3735-406F-B516-FD3ABF749409}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{BFC196DB-D41F-49C9-898D-13F0332078E7} = {312353EA-EB37-49C5-A6AD-4B50CF325C8A}
{FCF6A2CF-3735-406F-B516-FD3ABF749409} = {312353EA-EB37-49C5-A6AD-4B50CF325C8A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7BA0119B-B244-47D4-9075-F7D7D5D8E7CE}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ The library is available on [NuGet](https://www.nuget.org/packages/CodeSculpt).

**Installation**

The library is available on [NuGet](https://www.nuget.org/packages/CodeSculpt.Wpf). Just search *CodeSculpt* in the **Package Manager GUI** or run the following command in the **Package Manager Console**:
The library is available on [NuGet](https://www.nuget.org/packages/CodeSculpt.Wpf). Just search *CodeSculpt.Wpf* in the **Package Manager GUI** or run the following command in the **Package Manager Console**:

Install-Package CodeSculpt.Wpf
9 changes: 9 additions & 0 deletions samples/CodeSculpt.Wpf.Sample/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="CodeSculpt.Wpf.Sample.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CodeSculpt.Wpf.Sample"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
10 changes: 10 additions & 0 deletions samples/CodeSculpt.Wpf.Sample/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

namespace CodeSculpt.Wpf.Sample;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}

10 changes: 10 additions & 0 deletions samples/CodeSculpt.Wpf.Sample/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
23 changes: 23 additions & 0 deletions samples/CodeSculpt.Wpf.Sample/CodeSculpt.Wpf.Sample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\CodeSculpt.Wpf\CodeSculpt.Wpf.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Views\" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions samples/CodeSculpt.Wpf.Sample/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Window x:Class="CodeSculpt.Wpf.Sample.MainWindow"
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:Attached="clr-namespace:CodeSculpt.Wpf.AttachedProperties;assembly=CodeSculpt.Wpf"
xmlns:vm="clr-namespace:CodeSculpt.Wpf.Sample.ViewModels"
xmlns:local="clr-namespace:CodeSculpt.Wpf.Sample"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<Window.DataContext>
<vm:MainViewModel />
</Window.DataContext>
<StackPanel>
<TextBox Attached:TextBoxProperties.IsNumericOnly="True"
Attached:TextBoxProperties.OnEnterKeyDown="{Binding TextBoxCommand}" />


<Button Content="Button with tooltip test"
ToolTip="Ciaooo"
IsEnabled="{Binding ElementName=disableButton, Path=IsChecked}"
Attached:ButtonProperties.DisabledToolTip="asdasd" />

<CheckBox x:Name="disableButton" />
</StackPanel>
</Window>
13 changes: 13 additions & 0 deletions samples/CodeSculpt.Wpf.Sample/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Windows;

namespace CodeSculpt.Wpf.Sample;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
22 changes: 22 additions & 0 deletions samples/CodeSculpt.Wpf.Sample/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace CodeSculpt.Wpf.Sample.ViewModels;

public partial class MainViewModel : ObservableObject
{
public MainViewModel()
{
TextBoxCommand = new RelayCommand(() =>
{
// Command logic
a = true;
});
}

public RelayCommand TextBoxCommand { get; }

[ObservableProperty]
private bool a;
}
72 changes: 72 additions & 0 deletions src/CodeSculpt.Wpf/AttachedProperties/ButtonProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Windows;
using System.Windows.Controls;

namespace CodeSculpt.Wpf.AttachedProperties;
public class ButtonProperties : DependencyObject
{
#region DisableToolTip

private static readonly DependencyPropertyKey originalToolTipPropertyKey = DependencyProperty.RegisterAttachedReadOnly("OriginalToolTip", typeof(string), typeof(ButtonProperties), new FrameworkPropertyMetadata(default(string)));

/// <summary>
/// Contains the original Button.ToolTip value to display when the Button.IsEnabled property value is set to true.
/// </summary>
public static readonly DependencyProperty OriginalToolTipProperty = originalToolTipPropertyKey.DependencyProperty;

/// <summary>
/// Gets the value of the OriginalToolTip property.
/// </summary>
/// <param name="dependencyObject">The DependencyObject to return the OriginalToolTip property value from.</param>
/// <returns>The value of the OriginalToolTip property.</returns>
public static string GetOriginalToolTip(DependencyObject dependencyObject) => (string)dependencyObject.GetValue(OriginalToolTipProperty);

/// <summary>
/// Provides Button controls with an additional tool tip property that only displays when the Button.IsEnabled property value is set to false.
/// </summary>
public static readonly DependencyProperty DisabledToolTipProperty = DependencyProperty.RegisterAttached("DisabledToolTip", typeof(string), typeof(ButtonProperties), new UIPropertyMetadata(string.Empty, OnDisabledToolTipChanged));

/// <summary>
/// Gets the value of the DisabledToolTip property.
/// </summary>
/// <param name="dependencyObject">The DependencyObject to return the DisabledToolTip property value from.</param>
/// <returns>The value of the DisabledToolTip property.</returns>
public static string GetDisabledToolTip(DependencyObject dependencyObject) => (string)dependencyObject.GetValue(DisabledToolTipProperty);

/// <summary>
/// Sets the value of the DisabledToolTip property.
/// </summary>
/// <param name="dependencyObject">The DependencyObject to set the DisabledToolTip property value of.</param>
/// <param name="value">The value to be assigned to the DisabledToolTip property.</param>
public static void SetDisabledToolTip(DependencyObject dependencyObject, string value) => dependencyObject.SetValue(DisabledToolTipProperty, value);

private static void OnDisabledToolTipChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var button = dependencyObject as Button;
ToolTipService.SetShowOnDisabled(button, true);
var oldValue = e.OldValue as string;
var newValue = e.NewValue as string;

if (oldValue!.Length == 0 && newValue!.Length > 0)
{
button!.IsEnabledChanged += Button_IsEnabledChanged;
}
else if (oldValue.Length > 0 && newValue!.Length == 0)
{
button!.IsEnabledChanged -= Button_IsEnabledChanged;
}
}

private static void Button_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var button = sender as Button;

if (GetOriginalToolTip(button!) == null)
{
button!.SetValue(originalToolTipPropertyKey, button.ToolTip.ToString());
}

button!.ToolTip = (bool)e.NewValue ? GetOriginalToolTip(button!) : GetDisabledToolTip(button!);
}

#endregion
}
Loading

0 comments on commit 4df4258

Please sign in to comment.