您的位置:首页 > 其它

WPF(附加属性 Slider)

2013-04-11 14:15 190 查看
<Window x:Class="TestOfAttachPropertyOfSlider.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">
<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#代码

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
});
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: