TextBlock 绑定Command 当按钮用

今天遇到一个比较奇特但是也很合理的需求 就是拿TextBlock当按钮用 由于TextBlock不能直接Binding Command 所以要稍加变通一下

1.准备 引用System.Windows.Interactivity

添加命名空间  xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

2.实例

        <TextBlock Text="查询" FontSize="12" FontWeight="Black" Foreground="White">
                    <i:Interaction.Triggers>
                           <i:EventTrigger EventName="MouseLeftButtonDown">
                                  <i:InvokeCommandAction Command="{Binding SearchCommand, Mode=OneWay}"/>
                    </i:EventTrigger>
                    </i:Interaction.Triggers>
        </TextBlock>

其实上面的代码我是用Blend写的,在这儿写下的目的就是为了给没有Blend的时候直接拷贝用的!

   SearchCommand = new RelayCommand(() =>
            {
                SearchChartData();
            });

此项目是在MVVMLight下创建的项目,所以要用RelayCommand 别忘了引用DLL

你可能感兴趣的:(TextBlock 绑定Command 当按钮用)