TreeView- MVVM:fire command on selectedItemChanged

来源:互联网 发布:怎样在淘宝上做代销 编辑:程序博客网 时间:2024/04/28 21:24
Hi experts,

I am new to silverlight.
I have a small project based on MVVM. I am using tree view. I want to know how can I fire a command, which is defined in command.cs file, on selectedItemChange of tree view control.
my XAML file is as below
---------------------------------------------------XAML---------------------
<sdk:TreeView Height="230" HorizontalAlignment="Left" Name="trvWOList" VerticalAlignment="Top" Width="315" Margin="5"
ItemsSource="{Binding Path=lstWODetails, Mode=TwoWay}">
<sdk:TreeView.ItemTemplate>
<sdk:HierarchicalDataTemplate ItemsSource="{Binding Path=lstItemCodesTree, Mode=TwoWay}">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding strWOParent}">

</TextBlock>
<TextBlock Text="{Binding strItemCode}"></TextBlock>
<TextBlock Text="{Binding strstage}"></TextBlock>
</StackPanel>
</sdk:HierarchicalDataTemplate>
</sdk:TreeView.ItemTemplate>
</sdk:TreeView>

-----------------------END------------------------

this is the command declaration I have in my viewmodel file
-----------------------------------ViewModel--------------

public ICommand test { get { return new check(); } }
----------------------END------------


below is my command definition

-------------------Command.cs----------------

public class check : ICommand
{

public bool CanExecute(object parameter)
{
return true;
}

public event EventHandler CanExecuteChanged;

public void Execute(object parameter)
{
MessageBox.Show("hi");
}
}

----------END-----------------
 
 
 
hello experts,

I resolved this issue.
I made a small change in XAML and it works. Hope it may help others.

<sdk:TreeView Height="230" HorizontalAlignment="Left" Name="trvWOList" VerticalAlignment="Top" Width="315" Margin="5"
ItemsSource="{Binding Path=lstWODetails, Mode=TwoWay}">

<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedItemChanged">
<i:InvokeCommandAction x:Name="abc" Command="{Binding Path=test, Mode=TwoWay}">
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>




<sdk:TreeView.ItemTemplate>
<sdk:HierarchicalDataTemplate ItemsSource="{Binding Path=lstItemCodesTree, Mode=TwoWay}">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding strWOParent}">

</TextBlock>
<TextBlock Text="{Binding strItemCode}"></TextBlock>
<TextBlock Text="{Binding strstage}"></TextBlock>
</StackPanel>
</sdk:HierarchicalDataTemplate>
</sdk:TreeView.ItemTemplate>
</sdk:TreeView>
 
原文地址:http://forums.codeguru.com/archive/index.php/t-507076.html

原创粉丝点击