如何将Slider值绑定到Silverlight中的Rectable高度(在WPF中工作)? [英] How can I bind a Slider value to a Rectable height in Silverlight (works in WPF)?

查看:114
本文介绍了如何将Slider值绑定到Silverlight中的Rectable高度(在WPF中工作)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码适用于WPF,但不适用于Silverlight。

This code works in WPF but not in Silverlight.

Silverlight中是否有任何解决方法可以让我将滑块值绑定到元素高度? Silverlight在这里的局限性是什么?

Are there any workarounds in Silverlight to enable to me to bind a slider value to element heights? What are the limitations of Silverlight here?

谢谢彼得解决这个问题, 具有在线演示和可下载代码的解决方案

Thanks Peter for solving this, for others: here is the solution with online demo and downloadable code.

<UserControl x:Class="Second12.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
    Width="300" Height="200">
    <Grid Background="Tan">
        <StackPanel>
            <Canvas>
                <Border Background="#2200ffff" 
            Canvas.Left="40" 
            Canvas.Top="30" 
            CornerRadius="5" 
            BorderBrush="Brown"
            BorderThickness="1">
                    <Rectangle 
            Height="{Binding ElementName=theSlider, Path=Value}"
            Width="50"/>
                </Border>
            </Canvas>
        </StackPanel>
        <Slider Name="theSlider" HorizontalAlignment="Left" Width="200" Cursor="Hand"/>
    </Grid>
</UserControl>


推荐答案

元素绑定不是目前在Silverlight 2中支持。此MSDN文档文章对于Silverlight所提供的WPF功能的具体细节非常简洁。

ElementName binding is not currently supported in Silverlight 2. This MSDN documentation article is quite succinct on the specifics of the WPF functionality that Silverlight does and doesn't currently provide.

在此期间,您必须使用某种中介属性是一个完整的例子:

In the interim you have to use some sort of intermediary property, here is a full example:

Page.xaml:

Page.xaml:

<UserControl x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">

    <StackPanel x:Name="LayoutRoot" Background="AliceBlue">

        <Slider x:Name="Slider1" Minimum="10" SmallChange="10"
                LargeChange="20" Maximum="100"
                Value="{Binding HelperValue, Mode=TwoWay}" Cursor="Hand"/>

        <Rectangle x:Name="Rectangle1" Fill="Red"
                Height="{Binding HelperValue, Mode=OneWay}" Width="100" />

    </StackPanel>

</UserControl>

Page.xaml.cs:

Page.xaml.cs:

using System;
using System.ComponentModel;
using System.Windows.Controls;

namespace SilverlightApplication1
{
    public partial class Page : UserControl
    {
        BindingConduit<double> sliderToRect = new BindingConduit<double>(50.0);

        public Page()
        {
            InitializeComponent();

            LayoutRoot.DataContext = sliderToRect;
        }
    }

    public class BindingConduit<T> : INotifyPropertyChanged where T : struct
    {
        private T helperValue;

        public T HelperValue
        {
            get { return this.helperValue; }
            set
            {
                if ((this.helperValue.Equals(value) != true))
                {
                    this.helperValue = value;
                    this.RaisePropertyChanged("HelperValue");
                }
            }
        }

        public BindingConduit(T defaultValue)
        {
            HelperValue = defaultValue;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler propertyChanged = this.PropertyChanged;

            if (propertyChanged != null)
            {
                propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

Slider的Value属性绑定到HelperValue并设置为TwoWay,Rectangle的Height绑定到相同的属性,但只需要OneWay。

The Value property of the Slider is bound to HelperValue and set to TwoWay, the Rectangle's Height is bound to the same property but only requires OneWay.

正在读取HelperValue属性BindingConduit< T>的实例在页面中创建的类名为sliderToRect。该实例被设置为父StackPanel控件的DataContext。 Slider1的Value属性的任何更改都反映在Rectangle1的Height中,因为HelperValue属性引发了PropertyChanged事件。

The HelperValue property is being read from the instance of the BindingConduit<T> class created in Page called sliderToRect. This instance is set as the DataContext of the parent StackPanel control. Any change in Slider1's Value property is reflected in the Height of Rectangle1 because the HelperValue property raises the PropertyChanged event.

BindingConduit< T>是我创建的一个自定义类,它实现INotifyPropertyChanged,并且还允许您指定HelperValue的默认值(即在这种情况下,Slider1的inital Value和Rectangle1的高度(50.0))。

BindingConduit<T> is a custom class I've created that implements INotifyPropertyChanged and also lets you specify a default value for HelperValue (i.e. the inital Value of Slider1 and the Height of Rectangle1 (50.0) in this case).

这篇关于如何将Slider值绑定到Silverlight中的Rectable高度(在WPF中工作)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆