0

WPF で簡単なスピンボックス (numericUpDown) コントロールを作成しました (何もないため)。

モデルでデータバインディングを作成したいカスタム値プロパティを作成しました。

<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 binding can only set on a dependencyproperty of an dependencyobject

モデル プロパティは、次のように記述された C++/CLI です。

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

このプロパティを使用したバインドは、他のコントロール (テキスト ボックス、ラベル) に対して機能します。

問題は、カスタム SpinBox と Value プロパティの作成方法にあると思います。ウェブを掘り下げて一日を過ごした後、私は他に何をすべきかを見つけていません。

4

1 に答える 1