两个自定义UserControl之间的数据绑定

时间:2015-07-28 21:49:52

标签: c# wpf xaml

我有两个需要相互交互的自定义​​NumberUpDown用户控件。左边一个的最大值需要是右边一个的最小值。为了实现这一点,我将它们放在另一个包含两者并具有所有必需数据成员的用户控件中。

容器用户控件的xaml看起来像

<controls:NumberBox x:Name="LeftNumberBox" HorizontalAlignment="Right" Width="50" Min="{Binding Min}" Current="{Binding Left}" Max="{Binding Right}"/>
<controls:NumberBox x:Name="RightNumberBox" Grid.Column="2" HorizontalAlignment="Left" Width="50" Min="{Binding Left}" Current="{Binding Right}" Max="{Binding Max}"/>

和它的cs文件看起来像

public ThresholdBox()
{
    InitializeComponent();
}

public int Min
{
    get
    {
        return (int)GetValue(MinIntProperty);
    }
    set
    {
        SetValue(MinIntProperty, value);
    }
}

public static readonly DependencyProperty MinIntProperty =
    DependencyProperty.Register("Min", typeof(int), typeof(ThresholdBox), new PropertyMetadata(0));

public int Max
{
    get
    {
        return (int)GetValue(MaxIntProperty);
    }
    set
    {
        SetValue(MaxIntProperty, value);
    }
}

public static readonly DependencyProperty MaxIntProperty =
    DependencyProperty.Register("Max", typeof(int), typeof(ThresholdBox), new PropertyMetadata(100));

public int Left
{
    get
    {
        return LeftNumberBox.Current;
    }
    set
    {
        LeftNumberBox.Current = value;
    }
}

public int Right
{
    get
    {
        if(RightNumberBox != null)
        {
            return RightNumberBox.Current;
        }
        else
        {
            return 10;
        }
    }
    set
    {
        RightNumberBox.Current = value;
    }
}

和NumberBox又名NumberUpDown用户控件cs文件看起来像

public NumberBox()
{
    InitializeComponent();
    NUDTextBox.Text = Current.ToString();

    Min = 0;
    Current = 10;
    Max = 100;
}

public int Min
{
    get
    {
        return (int)GetValue(MinIntProperty);
    }
    set
    {
        SetValue(MinIntProperty, value);
    }
}

public static readonly DependencyProperty MinIntProperty =
    DependencyProperty.Register("Min", typeof(int), typeof(NumberBox), new PropertyMetadata(0));

public int Current
{
    get
    {
        return (int)GetValue(CurrentIntProperty);
    }
    set
    {
        SetValue(CurrentIntProperty, value);
    }
}

public static readonly DependencyProperty CurrentIntProperty =
    DependencyProperty.Register("Current", typeof(int), typeof(NumberBox), new PropertyMetadata(10));

public int Max
{
    get
    {
        return (int)GetValue(MaxIntProperty);
    }
    set
    {
        SetValue(MaxIntProperty, value);
    }
}

public static readonly DependencyProperty MaxIntProperty =
    DependencyProperty.Register("Max", typeof(int), typeof(NumberBox), new PropertyMetadata(100));

我没有包含按钮逻辑,因为它按预期工作

使用

调用容器
<controls:ThresholdBox Left="10" Right="90" Min="0" Max="100" Padding="0,20,0,20"/>

不幸的是,代码无法正常工作。两个NumberBox都从10开始,左边的一个将下降到0并且最多为10,而右边的一个上升到100并且不低于10,即使我在UI中将左边的NumberBoxes值降低到0。

我认为这可能是一个双向绑定问题,但会导致StackOverFlow异常。我在绑定代码中缺少什么使两个NumberBox分别使用/修改容器的Left和Right属性?

1 个答案:

答案 0 :(得分:0)

如果&#34;左边一个的最大值需要是右边一个的最小值&#34;,为什么这两个属性不能绑定到相同的值? 向ViewModel添加新属性:

public int Intermediate { get; set; }

然后将Max Max和right Min绑定到此属性。

<controls:NumberBox x:Name="LeftNumberBox" HorizontalAlignment="Right" Width="50" Min="{Binding Min}" Current="{Binding Left}" Max="{Binding Intermediate}"/>
<controls:NumberBox x:Name="RightNumberBox" Grid.Column="2" HorizontalAlignment="Left" Width="50" Min="{Binding Intermediate}" Current="{Binding Right}" Max="{Binding Max}"/>