1

カスタムコントロールは次のようになります。

generic.xaml

<Style TargetType="controls:MyControl">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="controls:MyControl">
        <Grid>
          <Grid.RowDefinitions>
            <RowDefinition Height="20" />
            <RowDefinition Height="20" />
          </Grid.RowDefinitions>
          <TextBox Grid.Row="0"
                   Text="{Binding ElementName=slider, Path=Value}" />
          <Slider Grid.Row="1" Name="slider" Width="120"
                  Minimum="1" Maximum="12"
                  Value="{Binding Mode=TwoWay,
                          RelativeSource={RelativeSource TemplatedParent},
                          Path=Value}"/>
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

MyControl.cs

public static readonly DependencyProperty ValueProperty =
  DependencyProperty.Register("Value",
  typeof(double),
  typeof(MyControl),
  new PropertyMetadata(0d, OnValueChanged));

  public double Value
  {
    get { return (double)base.GetValue(ValueProperty); }
    set { base.SetValue(ValueProperty, value); }
  }

  private static void OnValueChanged(DependencyObject source,
                                     DependencyPropertyChangedEventArgs e)
  {
    MyControl myControl = (MyControl)source;
    myControl.OnValueChanged((double)e.OldValue, (double)e.NewValue);
  }

  protected virtual void OnValueChanged(double oldValue, double newValue)
  {
    double coercedValue = CoerceValue(newValue);
    if (coercedValue != newValue)
    {
      this.Value = coercedValue;
    }
  }

  private double CoerceValue(double value)
  {
    double limit = 7;
    if (value > limit)
    {
      return limit;
    }
    return value;
  }

TextBoxは、値を表示するための単なるダミーです。

これで、このコントロールをアプリケーションに追加すると、DependencyPropertyの値が7に設定されていても、Sliderの値を7より大きく設定できます。

私が間違っていることは何ですか?TwoWayBindingはこの状況では機能しませんか?

前もって感謝します

4

1 に答える 1

1

私の再現のためのステップ:-

  • VS2010でSilverlightApplication1を呼び出して新しいSilverlightアプリケーションを作成します。
  • 新しい「SilverlightTemplatedControl」をSilverlightプロジェクトに追加し、「MyControl」という名前を付けます。
  • 内部コンテンツまたはControlTemplateをthemes/Generic.xamlファイルのControlTemplateにコピーしました。この汎用ファイル全体は次のようになります。-

    <Style TargetType="local:MyControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:MyControl">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="20" />
                            <RowDefinition Height="20" />
                        </Grid.RowDefinitions>
                        <TextBox Grid.Row="0"
                       Text="{Binding ElementName=slider, Path=Value}" />
                        <Slider Grid.Row="1" Name="slider" Width="120"
                      Minimum="1" Maximum="12"
                      Value="{Binding Mode=TwoWay,
                              RelativeSource={RelativeSource TemplatedParent},
                              Path=Value}"/>
                    </Grid>
    
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    • MyControl.csに配置されたC#をコピーしました。ファイル全体は次のようになります:-

System.Windowsを使用します。System.Windows.Controlsを使用する;

namespace SilverlightApplication1
{
    public class MyControl : Control
    {
        public MyControl()
        {
            this.DefaultStyleKey = typeof(MyControl);
        }

        public static readonly DependencyProperty ValueProperty =
  DependencyProperty.Register("Value",
  typeof(double),
  typeof(MyControl),
  new PropertyMetadata(0d, OnValueChanged));

        public double Value
        {
            get { return (double)base.GetValue(ValueProperty); }
            set { base.SetValue(ValueProperty, value); }
        }

        private static void OnValueChanged(DependencyObject source,
                                           DependencyPropertyChangedEventArgs e)
        {
            MyControl myControl = (MyControl)source;
            myControl.OnValueChanged((double)e.OldValue, (double)e.NewValue);
        }

        protected virtual void OnValueChanged(double oldValue, double newValue)
        {
            double coercedValue = CoerceValue(newValue);
            if (coercedValue != newValue)
            {
                this.Value = coercedValue;
            }
        }

        private double CoerceValue(double value)
        {
            double limit = 7;
            if (value > limit)
            {
                return limit;
            }
            return value;
        }

    }
}
  • MyControlのインスタンスをMainPage.xamlに追加しました。これは、次のようになります。-

    <UserControl x:Class="SilverlightApplication1.MainPage"
        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:SilverlightApplication1"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400">
    
        <Grid x:Name="LayoutRoot" Background="White">
            <local:MyControl />
         </Grid>
    
     </UserControl>
    
  • ソリューションを実行し、正常に動作します。

于 2011-09-01T13:14:08.257 に答える