Is there an existing way to use arbitrary C # expressions in XAML?

I would like to be able to use arbitrary C # expressions in XAML. Typically, this would be a property calculation for a user interface element based on two related values.

For example, calculating the width of a UI element based on two other properties.

This is a contrived example of what I would like for XAML to look like this:

<TextBox x:Name="textBox1" /> <TextBox x:Name="textBox2" /> <Rectangle Height={Double.Parse(textBox1.Text) + Double.Parse(textBox2.Text)} /> 

Of course, in XAML there is no built-in way to do this.

I know that I could use MultiBinding in combination with a custom converter, and this is usually the way I do this. However, it seems to me that it would be much easier to just add C # code to XAML, and I was wondering if someone out there had already solved this problem with the XAML extension or something else.

+8
c # wpf xaml
source share
5 answers

You embed the C # code in XAML as follows:

  <x:Code> <![CDATA[ void ButtonOnClick(object sender, RoutedEventArgs args) { Button btn = sender as Button; MessageBox.Show("The button labeled '" + btn.Content + "' has been clicked.","Information Message"); } ]]> </x:Code> 

But this approach is not recommended at all, as it mixes a pure presentation layer with business logic.

+2
source share

I saw custom Xaml converters that accept IronPython code and invoke DLR. This is not exactly C #, but it is certainly less ugly than using the [CDATA] tags.

http://pybinding.codeplex.com/

This is a link to an open source project on this subject.

+2
source share

Please note that with type code

 Height={Double.Parse(textBox1.Text) + Double.Parse(textBox2.Text)} 

it is especially difficult (although not entirely impossible, given Linq's considerations) to overestimate the value as soon as some of the operands change. Automatically updating the target value when the source changes is one of the main benefits of WPF bindings.

+1
source share

Wrap your expression in a public property and bind it to this property.

In C # codebehind:

 public double Heigth { get { return Double.Parse(textBox1.Text) + Double.Parse(textBox2.Text); } } 

In XAML:

 <Rectangle Height={Binding Heigth} /> 
+1
source share

Now I have an answer to my question. This is not the answer I was originally looking for, and it is a bit long, but it works.

I read this post by Josh Smith. He recommends not using value converters, but pushing calculations into the view model and exposing them as a property:

http://groups.google.com/group/wpf-disciples/browse_thread/thread/3fe270cd107f184f?pli=1

In my case, the text for "textBox1" and "textBox2" should be attached to the view model, so when they change in the view model, I know that it takes time to calculate and update the dependent property. The dependent property then fires its modified property event and updates from it.

If you have a case where you want an expression to depend on read-only control properties that you cannot easily bind to a view model, you can follow these tips:

Pushing read-only GUI properties back to ViewModel

I would still like to be able to embed (not business logic) expressions in XAML. But, seeing that this is not a built-in way to do this is likely to be a little hack. Going through the presentation model seems to be the right way to do this, but maybe one day I will be experimenting with writing a markup extension that allows expressions in XAML.

0
source share

Source: https://habr.com/ru/post/649792/


All Articles