All Projects → DingpingZhang → WpfExtensions

DingpingZhang / WpfExtensions

Licence: MIT license
Some syntactic sugar for Wpf development.

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to WpfExtensions

Reactivehistory
Reactive undo/redo framework for .NET.
Stars: ✭ 82 (-35.94%)
Mutual labels:  xaml, reactive, wpf
Calcbinding
Advanced WPF Binding which supports expressions in Path property and other features
Stars: ✭ 425 (+232.03%)
Mutual labels:  xaml, binding, wpf
IconPacks.Browser
The Browser for all available Icon packages from MahApps.Metro.IconPacks
Stars: ✭ 74 (-42.19%)
Mutual labels:  xaml, wpf
AdminRunasMenu
A WPF menu driven by powershell to perform administrator functions
Stars: ✭ 26 (-79.69%)
Mutual labels:  xaml, wpf
SharpDiskSweeper
Reveal outstanding files or folders that are eating up your disk space
Stars: ✭ 21 (-83.59%)
Mutual labels:  xaml, wpf
Toast
🍞 The rounded and animated Android Toast for .NET WPF/XAML
Stars: ✭ 25 (-80.47%)
Mutual labels:  xaml, wpf
FileRenamerDiff
A File Renamer App featuring a difference display before and after the change.
Stars: ✭ 32 (-75%)
Mutual labels:  xaml, wpf
Taskban
A personal productivity tool developed with C# and XAML.
Stars: ✭ 56 (-56.25%)
Mutual labels:  xaml, wpf
Code Samples
Just some code samples for MahApps and other experiments...
Stars: ✭ 205 (+60.16%)
Mutual labels:  xaml, wpf
reactive-states
Reactive state implementations (brainstorming)
Stars: ✭ 51 (-60.16%)
Mutual labels:  reactive, reactivity
XamlIslands
Repository with several XAML Islands v1 samples (Win32, WPF, and WinForms) to demonstrate how to use it.
Stars: ✭ 47 (-63.28%)
Mutual labels:  xaml, wpf
SimpleDialogs
💬 A simple framework to help displaying dialogs on a WPF app
Stars: ✭ 24 (-81.25%)
Mutual labels:  xaml, wpf
MahApps.Metro.netcoreapp30
.NET Core 3.1 MahApps.Metro sample
Stars: ✭ 15 (-88.28%)
Mutual labels:  xaml, wpf
Wpftoolkit
All the controls missing in WPF. Over 1 million downloads.
Stars: ✭ 2,970 (+2220.31%)
Mutual labels:  xaml, wpf
SpicyTaco.AutoGrid
A magical replacement for the built in WPF Grid and StackPanel
Stars: ✭ 67 (-47.66%)
Mutual labels:  xaml, wpf
Handycontrol
Contains some simple and commonly used WPF controls
Stars: ✭ 3,349 (+2516.41%)
Mutual labels:  xaml, wpf
ReactiveValidation
A small validation library for WPF and Avalonia which uses a fluent interface and allows display messages near controls in GUI with MVVM
Stars: ✭ 50 (-60.94%)
Mutual labels:  xaml, wpf
FriendEditor
A WPF sample app to demonstrate how to use MVVM design pattern and MVVMLight toolkit
Stars: ✭ 38 (-70.31%)
Mutual labels:  xaml, wpf
Gridextra
Custom panel controls for WPF/UWP.
Stars: ✭ 149 (+16.41%)
Mutual labels:  xaml, wpf
Modernwpf
Modern styles and controls for your WPF applications
Stars: ✭ 2,610 (+1939.06%)
Mutual labels:  xaml, wpf

WpfExtensions

English | 中文

This project comes from some scattered works I did while working on Wpf development, and is a supplement to existing Mvvm frameworks. They don't solve any major problems, they just provide some syntactic sugar and let people write a few lines of code less. Its services are not limited to Wpf development, other similar Xaml frameworks, such as Uwp, Maui, etc. should also be able to use, but I has never tested on other frameworks.

The project is structured as follows:

  • WpfExtensions.Xaml:A number of MarkupExtensions are provided to simplify Xaml development.
  • WpfExtensions.Binding:To simplify the code for property dependency updates, a function similar to the one in Vue.js for computed-property is provided.
  • WpfExtensions.Infrastructure:Some scattered features, when the time is ripe, will be separated out and released as separate modules.

NuGet

Package NuGet
WpfExtensions.Xaml version
WpfExtensions.Binding version

1. WpfExtensions.Binding

This module is designed to solve the problem of property update dependency, which is often encountered in development: the value of a property is calculated from multiple other property, then when these dependent properties changed, the resultant property also needs to notify the UI to update. For example: RectArea = Width * Height, all three properties in this formula need to be bound to the UI, and the display of the RectArea will be automatically refreshed when Width and Height are changed.

To achieve this effect, the traditional implementation is as follows, and it's not hard to find: it's quite a pain to write, and each dependent property has to add a line of code to notify the result property to refresh.

// View Model
public double Width {
    get => field;
    set {
        if (SetProperty(ref field, value)) {
            RaisePropertyChanged(nameof(RectArea));
        }
    }
}

public double Height {
    get => field;
    set {
        if (SetProperty(ref field, value)) {
            RaisePropertyChanged(nameof(RectArea));
        }
    }
}

public double RectArea => Width * Height;

So after using WpfExtensions.Binding, can it be shorter?

// View Model is derived from WpfExtensions.Binding.BindableBase.
public double Width {
    get => field;
    set => SetProperty(ref field, value);
}

public double Height {
    get => field;
    set => SetProperty(ref field, value);
}

public double RectArea => Computed(() => Width * Height);

Perhaps this example is too simple. If the same property affects multiple results, then multiple result properties have to be raised in that property. Such an intricate update dependency is not easy to maintain.

2. WpfExtensions.Xaml

0. *New CommandExtension

  • View (XAML):
<Element Command={markup:Command Execute} />
<Element Command={markup:Command ExecuteWithArgumentAsync, CanExecute}
         CommandParameter={Binding Argument} />
  • View Model (*.cs):
class ViewModel
{
    public void Execute() {}

    public void ExecuteWithArgument(string arg) {}

    // The `Execute` method supports async, and its default `Can Execute` method will disable the command when it is busy.

    public Task ExecuteAsync() => Task.Completed;

    public Task ExecuteWithArgumentAsync(string arg) => Task.Completed;

    // The `Can Execute` method does not support async.

    public bool CanExecute() => true;

    public bool CanExecuteWithArgument(string arg) => true;
}

1. ComposeExtension

Combine multiple Converters into one pipeline.

<TextBlock Visibility="{Binding DescriptionText, Converter={markup:Compose
                       {StaticResource IsNullOrEmptyOperator},
                       {StaticResource NotConverter},
                       {StaticResource BooleanToVisibilityConverter}}}"
           Text="{Binding DescriptionText}" />

2. IfExtension

Using the Conditional expression in XAML.

<Button Command="{markup:If {Binding BoolProperty},
                            {Binding OkCommand},
                            {Binding CancelCommand}}" />
<UserControl>
    <markup:If Condition="{Binding IsLoading}">
        <markup:If.True>
            <views:LoadingView />
        </markup:If.True>
        <markup:If.False>
            <views:LoadedView />
        </markup:If.False>
    </markup:If>
</UserControl>

3. SwitchExtension

Using the Switch expression in XAML.

<Image Source="{markup:Switch {Binding FileType},
                              {Case {x:Static res:FileType.Music}, {StaticResource MusicIcon}},
                              {Case {x:Static res:FileType.Video}, {StaticResource VideoIcon}},
                              {Case {x:Static res:FileType.Picture}, {StaticResource PictureIcon}},
                              ...
                              {Case {StaticResource UnknownFileIcon}}}" />
<UserControl>
    <Switch To="{Binding SelectedViewName}">
        <Case Label="View1">
            <views:View1 />
        </Case>
        <Case Label="{x:Static res:Views.View2}">
            <views:View2 />
        </Case>
        <Case>
            <views:View404 />
        </Case>
    </Switch>
</UserControl>

4. I18nExtension

Dynamically switch the culture resource without restarting the app.

<TextBlock Text="{markup:I18n {x:Static languages:UiStrings.MainWindow_Title}}" />
<TextBlock Text="{markup:I18nString {x:Static languages:UiStrings.SayHello}, {Binding Username}}" />
<TextBlock Text="{markup:I18nString {x:Static languages:UiStrings.StringFormat},
                                    {Binding Arg0},
                                    {Binding Arg1},
                                    ...,
                                    {Binding Arg15}}" />

5. StylesExtension (In Progress)

<Button Style="{markup:Styles {StaticResource FlatButtonStyle},
                              {StaticResource AnimationStyle},
                              ...}" />
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].