Coder Social home page Coder Social logo

strictxaml's Introduction

Strict XAML

XAML controls that follow strict guidelines to fix common issues.

Strict Combo Box

OK, so there's only one control in this library. But it fixes a pretty common problem.

When you data bind to both ItemsSource and SelectedItem, the default control will do something strange. When the ItemsSource changes so that the SelectedItem is no longer in the list, it will set SelectedItem to null. This even happens if you are just about to change SelectedItem to an item in the list. Furthermore, even if you check for null in your view model's property setter, the dependency property and your view model will simply disagree. You will be left with a combo box having nothing selected. There is no way to fix this issue in the view model itself.

The strict combo box follows this strict guideline:

  • All input was initiated by the user.
  • All output was initiated by the application.

The corollary being that the control itself cannot initiate property changes. In particular, it cannot set SelectedItem to null.

Using the StrictComboBox

Either copy the source code or install the NuGet package.

Install-Package StrictXAML

Add the strict namespace. Then bind to the StrictSelectedItem property instead of SelectedItem.

<Window x:Class="MyProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:strict="clr-namespace:StrictXAML;assembly=StrictXAML"
        Title="MainWindow">
    <Grid>
        <strict:StrictComboBox ItemsSource="{Binding Options}" StrictSelectedItem="{Binding SelectedOption}" />
    </Grid>
</Window>

All of the other base class properties are still available, including SelectedItem.

You can drop the guard clause from your property setter.

strictxaml's People

Contributors

acecoderlaura avatar michaellperry avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.