您的位置:首页 > 其它

TreeView- MVVM:fire command on selectedItemChanged

2012-05-16 18:24 567 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: