views:

15

answers:

2

I have binded an object to a WPF control. How can I toggle the object property "IsEditMode" on click of the edit button using only xaml and no code behind? Here is sample code of xaml -

<Label Style="{StaticResource TitleLabel}" 
       Content="{Binding Path=GroupTitle}" 
       Visibility="{Binding Path=IsEditMode, Converter={StaticResource boolToVis}}"
       HorizontalAlignment="Left" />

<Button Content="Edit" HorizontalAlignment="Right" VerticalAlignment="Center">
    <Button.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
            <!--Toggle the bindedobject.IsEditMode property of click of button-->
        </EventTrigger>
    </Button.Triggers>
</Button>
+1  A: 

using only xaml and no code behind

I don't think it's possible with no C# (or VB) code at all, but you can do it with no code-behind, using the MVVM pattern. So you will have C# code, just not in the code-behind...

If you go this way, you need to expose a command from your ViewModel:

    private DelegateCommand _enterEditModeCommand;
    public ICommand EnterEditModeCommand
    {
        get
        {
            if (_enterEditModeCommand== null)
            {
                _enterEditModeCommand= new DelegateCommand(EnterEditMode);
            }
            return _enterEditModeCommand;
        }
    }

    private void EnterEditMode()
    {
        IsEditMode = true;
    }

And bind your button to that command:

<Button Content="Edit" Command="{Binding EnterEditModeCommand}"
        HorizontalAlignment="Right" VerticalAlignment="Center">
Thomas Levesque
Can we hide label on button click?
Ramesh Soni
Yes, you just have to bind the label visibility to a property of the VM, and update that property in EnterEditMode
Thomas Levesque
A: 

There's a control for that already in the framework:

<Label Style="{StaticResource TitleLabel}" 
       Content="{Binding Path=GroupTitle}" 
       Visibility="{Binding Path=IsEditMode, Converter={StaticResource boolToVis}}"
       HorizontalAlignment="Left" />

<ToggleButton Content="Edit" HorizontalAlignment="Right" VerticalAlignment="Center"
       IsChecked="{Binding IsEditMode}"/>
John Bowen