WPF内嵌代码和后台代码简单混合使用

下面实例展示了WPF内嵌代码和后台代码混合使用,一个简单基础的实例:

xaml文件:

<Window x:Class="WPF内嵌代码和后台代码混合使用.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Background="BlanchedAlmond">
        <Canvas Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" Name="canvas1" VerticalAlignment="Top" Width="479">
            <TextBox Canvas.Left="147" Canvas.Top="73" Height="23" Name="textBox1" Width="120" />
            <TextBox Canvas.Left="147" Canvas.Top="121" Height="23" Name="textBox2" Width="120" />
            <TextBox Canvas.Left="147" Canvas.Top="173" Height="23" Name="textBox3" Width="120" />
            <Label Canvas.Left="93" Canvas.Top="171" Content="OUT:" Height="28" Name="label1" />
            <Label Canvas.Left="123" Canvas.Top="73" Content="X:" Height="28" Name="label2" />
            <Label Canvas.Left="123" Canvas.Top="119" Content="Y:" Height="28" Name="label3" />
            <Button Content="+" Height="25" Name="button1" Width="75" Click="button1_Click" Canvas.Left="26" Canvas.Top="230" />
            <Button Content="-" Height="25" Name="button2" Width="75" Canvas.Left="139" Canvas.Top="230" Click="button2_Click" />
            <Button Content="*" Height="25" Name="button3" Width="75" Canvas.Left="253" Canvas.Top="230" Click="button3_Click" />
            <Button Content="/" Height="25" Name="button4" Width="75" Canvas.Left="366" Canvas.Top="230" Click="button4_Click" />
            <x:Code>
                <![CDATA[    
                        void button1_Click(object sender, RoutedEventArgs e)   {    
                                    this.GetValue();    
                              }    
            
                        int Plus(int x ,int y){
                            return x +y;
                            }
                 void button2_Click(object sender, RoutedEventArgs e)   {    
                                    this.GetValue1();    
                              }    
            
                        int Minus(int x ,int y){
                            return x -y;
                            }
                void button3_Click(object sender, RoutedEventArgs e)   {    
                                    this.GetValue2();    
                              }    
            
                        int Multiply(int x ,int y){
                            return x *y;
                            }
                void button4_Click(object sender, RoutedEventArgs e)   {    
                                    this.GetValue3();    
                              }    
            
                        int Divide(int x ,int y){
                            return x /y;
                            }
                  ]]>
                
            </x:Code>
        </Canvas>
    </Grid>
</Window>

 

  后台简单代码:

   public void  GetValue() {
               this.textBox3.Text = this.Plus(int.Parse(this.textBox1.Text), int.Parse(this.textBox2.Text)).ToString();
              }
           public void GetValue1()
           {
               this.textBox3.Text = this.Minus(int.Parse(this.textBox1.Text), int.Parse(this.textBox2.Text)).ToString();
           }
           public void GetValue2()
           {
               this.textBox3.Text = this.Multiply(int.Parse(this.textBox1.Text), int.Parse(this.textBox2.Text)).ToString();
           }
           public void GetValue3()
           {
               this.textBox3.Text = this.Divide(int.Parse(this.textBox1.Text), int.Parse(this.textBox2.Text)).ToString();
           }

效果展示:

示例下载:https://files.cnblogs.com/BABLOVE/WPF%E5%86%85%E5%B5%8C%E4%BB%A3%E7%A0%81%E5%92%8C%E5%90%8E%E5%8F%B0%E4%BB%A3%E7%A0%81%E6%B7%B7%E5%90%88%E4%BD%BF%E7%94%A8.rar

posted @ 2013-08-02 00:18  如梦不是梦  阅读(682)  评论(0编辑  收藏  举报