具有短语法的Event Pass元素源

时间:2013-07-19 09:57:15

标签: caliburn.micro

这个方法工作按钮事件将TreeView元素传递给ViewModel但是没有传递短语法事件,问题出在哪里?感谢。

<Button Content="Search in TreeView" Height="34" Width="100">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <cal:ActionMessage MethodName="SearchTreeView">
                <cal:Parameter Value="{Binding ElementName=TreeView}" />
            </cal:ActionMessage>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>
<TreeView x:Name="TreeView" ItemsSource="{Binding TreeViewSource}"/>

短语法事件

<Button x:Name="SearchTreeView" Content="Search in TreeView" cal:Message.Attach="[Event Click] = [Action SearchTreeView($source.TreeView)]" />

<TreeView x:Name="TreeView" ItemsSource="{Binding TreeViewSource}"/>

1 个答案:

答案 0 :(得分:2)

您不需要指定$ source.Treeview,只需要元素的名称,TreeView就够了

<Button x:Name="SearchTreeView" Content="Search in TreeView" cal:Message.Attach="[Event Click] = [Action SearchTreeView(TreeView)]" />

<TreeView x:Name="TreeView" ItemsSource="{Binding TreeViewSource}"/>

也就是说,将控件传递给ViewModel并不是一个好主意。如果要对所选项执行某些操作,则应将TreeView的SelectedItem属性绑定到ViewModel上的属性,并从SearchTreeView方法访问该属性,例如

<Button x:Name="SearchTreeView" Content="Search in TreeView" cal:Message.Attach="[Event Click] = [Action SearchTreeView()]" />

<TreeView x:Name="TreeView" ItemsSource="{Binding TreeViewSource}" SelectedItem={Binding MySelectedItem}"/>