Bound Command not firing on another viewModel? What Am I doing wrong?
Posted
by devnet247
on Stack Overflow
See other posts from Stack Overflow
or by devnet247
Published on 2010-03-21T17:38:19Z
Indexed on
2010/03/21
17:41 UTC
Read the original article
Hit count: 474
wpf-binding
Hi
I cannot seem to bind a command to a button.I have a treeview on the left showing
Country
City etc..
And I tabcontrol on the right. do I This uses 4 viewModels
rootviewModel-ContinentViewModel-CountryViewModel-CityViewModel
What I am building is based on
http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx
Now on one of the tabs I have a Toolbar with a button "TestButton" that I have mapped in zaml. This does not fire!
The reason is not firing is because I m binding the RootViewModel but the command that is bound in zaml is in the cityViewModel.
How Do I pass the datacontext from one view to the other? or how do I make the button fire. I need the command to be in the cityViewModel.
Any Suggestions on how I bind it?
View "WorldExplorerView" where I bind the main DataContext
public partial class WorldExplorerView
{
public WorldExplorerView()
{
InitializeComponent();
var continents = Database.GetContinents();
var rootViewModel = new RootViewModel(continents);
DataContext = rootViewModel;
}
}
CityViewModel
public class CityViewModel : TreeViewItemViewModel
{
private City _city;
private RelayCommand _testCommand;
public CityViewModel(City city, CountryViewModel countryViewModel):base(countryViewModel,false)
{
_city = city;
}
Properties etc......
public ICommand TestCommand
{
get
{
if(_testCommand==null)
{
_testCommand = new RelayCommand(param => GetTestCommand(), param => CanCallTestCommand);
;
}
return _testCommand;
}
}
protected bool CanCallTestCommand
{
get { return true; }
}
private static void GetTestCommand()
{
MessageBox.Show("It works");
}
}
ZAML
<DockPanel>
<DockPanel LastChildFill="True">
<Label DockPanel.Dock="top" Content="Title " HorizontalAlignment="Center"></Label>
<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem Content="Status Bar" ></StatusBarItem>
</StatusBar>
<Grid DockPanel.Dock="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<TreeView Name="tree" ItemsSource="{Binding Continents}">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded,Mode=TwoWay}"/>
<Setter Property="IsSelected" Value="{Binding IsSelected,Mode=TwoWay}"/>
<Setter Property="FontWeight" Value="Normal"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type ViewModels:ContinentViewModel}"
ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Margin="3,0" Source="Images\Continent.png"/>
<TextBlock Text="{Binding ContinentName}"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type ViewModels:CountryViewModel}"
ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Margin="3,0" Source="Images\Country.png"/>
<TextBlock Text="{Binding CountryName}"/>
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type ViewModels:CityViewModel}" >
<StackPanel Orientation="Horizontal">
<Image Width="16" Height="16" Margin="3,0" Source="Images\City.png"/>
<TextBlock Text="{Binding CityName}"/>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
<GridSplitter Grid.Row="0" Grid.Column="1" Background="LightGray"
Width="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<Grid Grid.Column="2" Margin="5" >
<TabControl>
<TabItem Header="Demo">
<DockPanel LastChildFill="True">
<ToolBar DockPanel.Dock="Top">
<!-- DOES NOT WORK-->
<Button Name="btnTest" Command="{Binding TestCommand}" Content="Press me see if works"></Button>
</ToolBar>
<TextBox></TextBox>
</DockPanel>
</TabItem>
<TabItem Header="Details" DataContext="{Binding Path=SelectedItem.City, ElementName=tree, Mode=OneWay}">
<StackPanel >
<TextBlock VerticalAlignment="Center" FontSize="12" Text="{Binding CityName}"/>
<TextBlock VerticalAlignment="Center" FontSize="12" Text="{Binding Area}"/>
<TextBlock VerticalAlignment="Center" FontSize="12" Text="{Binding Population}"/>
<TextBlock VerticalAlignment="Center" FontSize="12" Text="{Binding CityDetailsInfo.ClubsCount}"/>
<TextBlock VerticalAlignment="Center" FontSize="12" Text="{Binding CityDetailsInfo.PubsCount}"/>
</StackPanel>
</TabItem>
</TabControl>
</Grid>
</Grid>
</DockPanel>
</DockPanel>
© Stack Overflow or respective owner