欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WPF(附加属性 Slider)

程序员文章站 2022-12-01 17:42:16
[html]

[html]
<Window x:Class="TestOfAttachPropertyOfSlider.MainWindow" 
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" Height="350" Width="525"> 
    <Canvas > 
        <Slider x:Name="sliderX"  
                Canvas.Top="10" 
                Canvas.Left="10" 
                Width="260" 
                Minimum="50" 
                Maximum="200" /> 
        <Slider x:Name="sliderY" 
                Canvas.Top="40" 
                Canvas.Left="10" 
                Width="260" 
                Minimum="50" 
                Maximum="200" /> 
         
        <Rectangle x:Name="rect" Fill="Blue" 
                   Width="30" Height="30" 
                   Canvas.Left="{Binding ElementName=sliderX,Path=Value}" 
                   Canvas.Top="{Binding ElementName=sliderY,Path=Value}" /> 
         
    </Canvas> 
</Window> 

<Window x:Class="TestOfAttachPropertyOfSlider.MainWindow"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Canvas >
        <Slider x:Name="sliderX"
                Canvas.Top="10"
                Canvas.Left="10"
                Width="260"
                Minimum="50"
                Maximum="200" />
        <Slider x:Name="sliderY"
                Canvas.Top="40"
                Canvas.Left="10"
                Width="260"
                Minimum="50"
                Maximum="200" />
       
        <Rectangle x:Name="rect" Fill="Blue"
                   Width="30" Height="30"
                   Canvas.Left="{Binding ElementName=sliderX,Path=Value}"
                   Canvas.Top="{Binding ElementName=sliderY,Path=Value}" />
       
    </Canvas>
</Window>
----------------------------------------------------------------------------------->

等效的C#代码


[csharp] 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
 
namespace TestOfAttachPropertyOfSlider 

    /// <summary>  
    /// Interaction logic for MainWindow.xaml  
    /// </summary>  
    public partial class MainWindow : Window 
    { 
        public MainWindow() 
        { 
            InitializeComponent(); 
 
            this.rect.SetBinding(Canvas.LeftProperty, new Binding("Value") 
                                                          { 
                                                              Source = sliderX 
                                                          }); 
            this.rect.SetBinding(Canvas.RightProperty, new Binding("Value") 
                                                           { 
                                                               Source = sliderY 
                                                           }); 
        } 
    } 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestOfAttachPropertyOfSlider
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.rect.SetBinding(Canvas.LeftProperty, new Binding("Value")
                                                          {
                                                              Source = sliderX
                                                          });
            this.rect.SetBinding(Canvas.RightProperty, new Binding("Value")
                                                           {
                                                               Source = sliderY
                                                           });
        }
    }
}