Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Reflections On WPF
Message
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Miscellaneous
Thread ID:
01466107
Message ID:
01466201
Views:
52
>[ ] IMHO, things like bindings and commands should be done in the code behind, because XAML doesn't provide for any logic. While you can declare bindings in the XAML, you can't do it conditionally.
>
>Not sure what you mean on this one. I tend to declare most bindings in XAML - if the binding fails then it fails silently (i.e. no exception is thrown) and that, oddly enough, can be beneficial - if the binding makes sense then it's used; if it doesn't then the control is disabled......you can use that behaviour to your advantage

>
>How would you do this in XAML?
>
>if condition A
>  bind to class X
>else
>  bind to class Y
>
>Again, I think binding is the realm of the developer not the designer, and XAML is the realm of the designer, not the developer. I find it MUCH easier to do things like commands and bindings in the code behind.
>
>While you might be able to do some things like this in XAML you can't do it all, which means some of your logic is now in the XAML and some in C#, which would get ugly fast.

If we're talking MVVM in it's purest form then the goal is to use *only* XAML in the UI. Having C# code in the View as well as the VM is what becomes messy. Also, with MVVM, binding in the VM is a no-no - the VM should know *nothing* about the View so, by definition, won't know what needs to be bound......

In your example above I'd use XAML to bind to a property on the VM and that property would hold (or point to) the relevant class.
>
>Also, I don't know about you, but I WANT to know when a binding fails. Why would you want a silent failure to go unreported?
Simple example:
<Window x:Class="Junk.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock Width="100"  Text="{Binding ElementName=C,Path=Tag.Width}" />
            <TextBlock Width="100"  Text="{Binding ElementName=C,Path=Tag.RadiusX}"/>
        </StackPanel>
        <Canvas x:Name="C">
            <Ellipse Width="100" Height="100" Fill="Red" MouseDown="MouseDown"/>                
            <Rectangle Canvas.Top="120" Fill="Green" Width="120" Height="100" RadiusX="5" MouseDown="MouseDown"></Rectangle>
        </Canvas>
    </StackPanel>
</Window>
Just 'C.Tag=sender;' in the MouseDown event. RadiusX will be displayed when it is a valid property of the selected shape but not otherwise which is fine by me and I wouldn't want that occurrence to throw an exception....
Previous
Reply
Map
View

Click here to load this message in the networking platform