2017-10-05 4 views
0

WPFで単純なスピンボックス(numericUpDown)コントロールを作成しました(存在しないため)。カスタムプロパティでデータバインドを設定する方法

私は、Modelを使ってデータバインディングを作成するカスタムValueプロパティを作成しました。ここで

<UserControl x:Class="PmFrameGrabber.Views.SpinBox" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:local="clr-namespace:PmFrameGrabber.Views" 
     mc:Ignorable="d" 
     d:DesignHeight="25" d:DesignWidth="100"> 
<UserControl.Resources> 
    <local:IntToStringConv x:Key="IntToStringConverter" /> 
</UserControl.Resources> 
<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition /> 
     <ColumnDefinition Width="25" /> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition /> 
     <RowDefinition /> 
    </Grid.RowDefinitions> 
    <TextBox Name="TbValue" Grid.RowSpan="2" HorizontalContentAlignment="Right" 
      VerticalContentAlignment="Center" HorizontalAlignment="Stretch" 
      VerticalAlignment="Stretch" Text="{Binding Value, Converter={StaticResource IntToStringConverter}}"/> 
    <Button Name="BtPlus" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Stretch" Margin="3,0,0,0" 
      VerticalAlignment="Center" FontSize="8" Content="+" Click="BtPlus_Click" /> 
    <Button Name="BtMinus" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Stretch" Margin="3,0,0,0" 
      VerticalAlignment="Center" FontSize="8" Content="-" Click="BtMinus_Click" /> 
</Grid> 
</UserControl> 

背後にあるコードです:

public partial class SpinBox : UserControl 
{ 
    public static DependencyProperty ValueDP = 
     DependencyProperty.Register("Value", typeof(int), typeof(SpinBox), new UIPropertyMetadata(0)); 

    // Public bindable properties 
    public int Value 
    { 
     get => (int)GetValue(ValueDP); 
     set => SetValue(ValueDP, value); 
    } 

    public SpinBox() 
    { 

     InitializeComponent(); 
     DataContext = this; 
    } 
    private void BtPlus_Click(object sender, RoutedEventArgs e) => Value++; 

    private void BtMinus_Click(object sender, RoutedEventArgs e) => Value--; 
} 

は、私はこのようなコントロールを使用しようとしています別のビューでは:

ここ
<local:SpinBox Width="80" Height="25" Value="{Binding Cam.ExposureTime, Mode=TwoWay}" /> 

私はエラーを取得する: WPFバインディング依存オブジェクトの依存プロパティでのみ設定できます

property int ExposureTime 
{ 
    void set(int value) 
    { 
     m_settings->exposureTime = value; 
     OnPropertyChanged(GetPropName(Camera, ExposureTime)); 
    } 
    int get() 
    { 
     return m_settings->exposureTime; 
    } 
} 

は、他のコントロールのために、このプロパティの作品(テキストボックス、ラベル)との結合:モデルプロパティは次のように書かれたC++/CLIです。

私のカスタムSpinBoxと、私がValueプロパティを作成した方法で問題が発生すると思います。ウェブを掘る日の後、私は何をすべきかを見つけられませんでした。

答えて

1

は、あなたがたDependencyPropertyフィールドは<PropertyName>Property命名されなければならないことを要求する依存関係プロパティの命名規則、無視している:ユーザーコントロールのコンストラクタで

DataContext = this; 

を設定することに加えて

public static readonly DependencyProperty ValueProperty = 
    DependencyProperty.Register("Value", typeof(int), typeof(SpinBox)); 

public int Value 
{ 
    get => (int)GetValue(ValueProperty); 
    set => SetValue(ValueProperty, value); 
} 

を、あなたのことを防ぎ継承されたDataContextにバインドできます。現在のDataContextにはCamプロパティが存在しないため

<local:SpinBox Value="{Binding Cam.ExposureTime, Mode=TwoWay}" /> 

のように結合すると、動作しません。

したがって、UserControlのDataContextを明示的に設定しないでください。

代わりに、このような「内部」バインディングを書く:

<TextBox Text="{Binding Value, 
       RelativeSource={RelativeSource AncestorType=UserControl}, ...}" /> 
+0

おかげで多くのことを、あなたのポイントは、すべての問題を解決しました。 – Safiron

関連する問題