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
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
相关文章推荐
- treeview OnSelectedNodeChanged js的方法
- 关于TREEVIEW的ONSELECTEDNODECHANGED事件
- WPF自定义treeview绑定事件SelectedItemChanged
- treeview用OnTreeNodePopulate动态加载后OnSelectedNodeChanged怎么获取节点值
- WPF TreeView SelectedItemChanged called twice
- 由TreeView 如何触发OnSelectedNodeChanged事件想到的与做到的
- Xamarin.iOS + MvvmCross: UIPickerView data binding, SelectedItemChanged event
- Xamarin.iOS + MvvmCross: UIPickerView data binding, SelectedItemChanged event
- 由TreeView 如何触发OnSelectedNodeChanged事件想到的与做到的
- 关于treeview的SelectedItemChanged事件和treeViewItem的Unselected事件
- ViewPager setCurrentItem(0)时,OnPageChangeListener onPageSelected() 不响应
- 解决方案:treeview右击菜单ContextMenu的MenuItem Command绑定不可用。
- Treeview SelectedNodeChanged event not firing
- webform treeview SelectedNodeChanged事件不响应
- WPF MVVM TreeViewItem
- Android ListView item 不触发onItemSelectChanged的解决方法
- DataList Web--OnItemCommand 事件
- 如何响应TreeView中的OnTreeNodeCheckChanged事件
- LVN_ITEMCHANGED(OnItemchanged)通知响应多次的问题
- MVVM里绑定TreeView控件的SelectedItem