当前位置:网站首页 / WPF / 正文

【ay wpf markup】AY XAML应该这样玩-和后台ViewModel交互,操作方法【17/18】

时间:2016年07月22日 | 作者 : aaronyang | 分类 : WPF | 浏览: 1794次 | 评论 0

====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========

===============无耻  推酷网 =================无德抓别人文章---=======


新建Window4ViewModel类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace WpfApplication3
{
    public class Window4ViewModel
    {
        public Window4ViewModel()
        {

        }
        public void Test1(int m, int n)
        {
            MessageBox.Show((m+n).ToString());
        }




    }
}

新建一个窗口Window4.xaml的Window

第一种引入Model

<Window x:Class="WpfApplication3.Window4"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication3"
        mc:Ignorable="d"
         xmlns:p="http://www.ayjs.net/markup"
       Title="DEMO4 www.ayjs.net" Height="800" Width="1000" WindowStartupLocation="CenterScreen">
    <Window.Resources>
        <local:Window4ViewModel x:Key="ViewModel"/>
    </Window.Resources>
    <Window.DataContext>
        <Binding Source="{StaticResource ViewModel}"/>
    </Window.DataContext>

    <Grid>
      
    </Grid>
</Window>

第二种更方便,最推荐的

 <Window.DataContext>
        <local:Window4ViewModel />
    </Window.DataContext>


====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========


步骤一:

    <Button Content="AY单击测试后台方法" Width="120" Height="40">
                <p:Attached.Operations>
                    <p:CallHandler Path="Click => Test1" SetHandled="True">
                        <p:Expr Value="1"></p:Expr>
                        <p:Expr Value="2"></p:Expr>
                    </p:CallHandler>
                </p:Attached.Operations>
            </Button>


单击时候,会调用

Window4ViewModel 类的Test1方法 通过Expr传递参数



步骤二,参数动态

        <StackPanel>
            <Button Content="AY单击测试后台方法" Width="120" Height="40">
                <p:Attached.Operations>
                    <p:CallHandler Path="Click => Test1" SetHandled="True">
                        <p:Expr Value="1"></p:Expr>
                        <p:Expr Value="2"></p:Expr>
                    </p:CallHandler>
                </p:Attached.Operations>
            </Button>
            <TextBox Width="80" Height="20" x:Name="tb1"></TextBox>
            <TextBox Width="80" Height="20" x:Name="tb2"></TextBox>
            <Button Content="AY单击测试后台方法2" Width="120" Height="40">
                <p:Attached.Operations>
                    <p:CallHandler Path="Click => Test1" SetHandled="True">
                        <p:Get Source="{Binding Text,ElementName=tb1}"></p:Get>
                        <p:Get Source="{Binding Text,ElementName=tb2}"></p:Get>
                    </p:CallHandler>
                </p:Attached.Operations>
            </Button>
        </StackPanel>

文本框输入2,第二个输入3,单击"AY单击测试后台方法2"按钮

弹出 对话框,显示5.



步骤三,ViewModel添加集合和ShowItem方法

             public Window4ViewModel()
        {
            ayjs = new ObservableCollection<string> { "wpf", "asp.net mvc", "blend" };
        }
        public ObservableCollection<string> ayjs { get; set; }
             public void ShowItem(string text)
        {
            MessageBox.Show(text);
        }


前台

      <ListBox Name="ayjsListBox" ItemsSource="{Binding ayjs}"/>
            <Button Content="AY 获得列表选中的值" Width="130" Height="40">
                <p:Attached.Operations>
                    <p:CallHandler Path="Click => ShowItem" SetHandled="True">
                        <p:Get Source="{Binding SelectedValue,ElementName=ayjsListBox}"></p:Get>
                    </p:CallHandler>
                </p:Attached.Operations>
            </Button>

效果:

比如:选中   wpf   然后单击按钮,会调用后台方法ShowItem方法


混用

 <ListBox Name="ayjsListBox" ItemsSource="{Binding ayjs}"/>
            <Button Content="AY 获得列表选中的值" Width="130" Height="40">
                <p:Attached.Operations>
                    <p:CallHandler Path="Click => ShowItem" SetHandled="True">
                        <p:Get Source="{Binding SelectedValue,ElementName=ayjsListBox}"></p:Get>
                    </p:CallHandler>
                    <p:ScriptHandler Path="MouseLeave">
                        @AssociatedObject.Background='Red';
                    </p:ScriptHandler>
                </p:Attached.Operations>
            </Button>



====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========



推荐您阅读更多有关于“WPF4.5,”的文章

猜你喜欢

额 本文暂时没人评论 来添加一个吧

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

  查看权限

抖音:wpfui 工作wpf

目前在合肥企迈科技公司上班,加我QQ私聊

2023年11月网站停运,将搬到CSDN上

AYUI8全源码 Github地址:前往获取

杨洋(AaronYang简称AY,安徽六安人)AY唯一QQ:875556003和AY交流

高中学历,2010年开始web开发,2015年1月17日开始学习WPF

声明:AYUI7个人与商用免费,源码可购买。部分DEMO不免费

查看捐赠

AYUI7.X MVC教程 更新如下:

第一课 第二课 程序加密教程

标签列表