Frage

Ich habe kürzlich entdeckt, dass WPF unterschiedliche Messeinheiten in XAML unterstützt. Neben den Standard-Dips gibt es auch Unterstützung für Pixel, Zoll und Zentimeter (soweit ich weiß). Dadurch kann der Designer XAML schreiben, z. B. dieses: generasacodicetagpre.

Sie können diese Werte jedoch nicht binden. Stellen Sie sich vor, wir haben ein Viewmodel mit Dimensionseigenschaft, das eine Zeichenfolge ist, zum Beispiel "7cm". Folgendes funktioniert nicht: generasacodicetagpre.

formatexception wird geworfen. In ähnlicher Weise, wenn er einen generationspflichtigen Erstellen in Code-Hinten erstellt, so: generasacodicetagpre.

Compilation schlägt fehl, da die Ausnahme in den Konstruktor geworfen wird / wo immer Sie versuchen, das Steuerelement zu erstellen.

Ich frage mich:

War es hilfreich?

Lösung

You can do this in code behind by applying the same conversion the XAML-parser uses, you do not need to reimplement this logic if you don't mind passing around strings:

button.Width = (double)new LengthConverter().ConvertFrom("2cm");

The XAML-parser uses TypeConverters to convert strings to the needed values, in the documentation page of the Width property you can see that it has a TypeConverterAttribute specifying that a LengthConverter should be used. This attribute can be used to locally override how a property should be handled, but it can also be applied at class level, so if an Employee class has this attribute specifying that an EmployeeConverter should be used said converter will be the default for properties of type Employee.

I am a bit surprised the binding does not apply this type converter, but using it inside an IValueConverter would be simple enough, in fact you could create a markup extension which constructs an IValueConverter from a type converter to make it nicely generic.

Andere Tipps

For 1) and 2) Because DIPs are device independent, you can calculate any "natural unit" to and from without problems. The exception are (unnatural :) device dependent pixels.

Create a ValueConverter that converts string values to doubles where your desired units may be specified in the string. Then you can use the ValueConverter in your Binding and calculate in your ViewModel in "natural units".

For 3) and the implementation: I have quickly googled and found nothing, but maybe will a deeper search show something. Otherwise, creating your own converter should not be very difficult. Here some conversion infos:

96DIP == 1inch == 2.54cm;
1cm == 37.8DIP;
1zoll == 1inch == 2.54cm;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top