views:

42

answers:

1

I have a problem with nested StackPanels. If I define StackPanels like in the code below, button binds to command (MVVM and command pattern) but doesn't react on button click (command function is not called). When I put stackPanel4 on second position (below stackPanel3) everything works ok. When I move stackPanel4 on the last position (I have more StackPanels then two inside parent StackPanel), button is not bound and command function is not called. What could be a reason for this behavior? If I set DataContext for stackPanel4 it works ok.

    <StackPanel DataContext="{StaticResource vmUserMasterData}" Grid.Column="1" Height="320" HorizontalAlignment="Left" Margin="4,6,4,0" Name="stackPanel1" VerticalAlignment="Top" Width="491">
        <StackPanel Height="40" Name="stackPanel4" Orientation="Horizontal" Width="440">
            <Button Content="Update User Data" Name="button1" Height="23" Width="440" Command="{Binding Path=UpdateDataCommand}"/>
        </StackPanel>
        <StackPanel Height="31" Name="stackPanel3" Orientation="Horizontal" Width="440">
            <sdk:Label Content="Username" Height="28" Name="label2" Width="74" />
            <TextBox Height="23" Name="textBox2" Width="365" Text="{Binding Username, Mode=OneWay}" />
        </StackPanel>
    </StackPanel>
A: 

There must be more to this problem. We have reproduced your setup and the command event fires every time, regardless or the order of the StackPanels (basically as you would expect it to work).

Can you provide any source from your ViewModel (specifically relating to how the command is created/used)? The description of the XAML, for the case that fails, is not clear. Can you provide an exact copy of the Xaml when it fails to fire? It might be a specific combination of panels you have on your machine. One wrong closure and it would fail (e.g. if it fell outside stackPanel1 by accident).

Our reproduction (which works regardless of panel order):

Xaml:

<UserControl.Resources>
    <SilverlightApplication1:ViewModel x:Key="vmUserMasterData"/>
</UserControl.Resources>
    <Grid x:Name="LayoutRoot">
        <StackPanel DataContext="{StaticResource vmUserMasterData}" Grid.Column="1" Height="320" HorizontalAlignment="Left" Margin="4,6,4,0" Name="stackPanel1" VerticalAlignment="Top" Width="491">
            <StackPanel Height="31" Name="stackPanel3" Orientation="Horizontal" Width="440">
                <sdk:Label Content="Username" Height="28" Name="label2" Width="74" />
                <TextBox Height="23" Name="textBox2" Width="365" Text="{Binding Username, Mode=OneWay}" />
            </StackPanel>
        <StackPanel Height="40" Name="stackPanel4" Orientation="Horizontal" Width="440">
            <Button Content="Update User Data" Name="button1" Height="23" Width="440" Command="{Binding Path=UpdateDataCommand}"/>
        </StackPanel>
    </StackPanel>
    </Grid>

ViewModel code:

using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;

namespace SilverlightApplication1
{
    public class ViewModel
    {
        public ICommand UpdateDataCommand { get; set; }
        public string Username { get; set; }

        public ViewModel()
        {
            Username = "Woof!";
            UpdateDataCommand = new DelegateCommand<string>(
                (x) =>
                {
                    int dog = 1;    // breakpoint here
                });
        }
    }
}
Enough already
stackPanel1 is inside Grid and it is all part of the ChildWindow control.
Aleksandar
Thanks for your answer. This is additional data: stackPanel1 is inside Grid and it is all part of the ChildWindow control. Also: <controls:ChildWindow.Resources> <local:ViewModelExample x:Key="vmExample"/> <local:ViewModelUserMasterData x:Key="vmUserMasterData"/> </controls:ChildWindow.Resources>vmExample is not used within these stackPanels. For command pattern I used pattern similar to RelayCommand pattern by Josh Smith. There is no problem with closing tags or similar. Everything seems ok in runtime; the only problem is with binding.
Aleksandar
@Aleksandar: I have posted our "cut-down to the minimum" sample for you to examine. Hopefully something will spring out at you, in your own code, for you to try. We can't do much more without full code at this point as we can't repro the problem.
Enough already
Thanks a lot guys. I rewritten again code, compiled again and now it works. Basically, it was the same like in your post and I don't realy know what I was doing wrong.
Aleksandar