用户控件的依赖属性没有绑定到ViewModel的属性

本文关键字:属性 绑定 ViewModel 依赖 用户 控件 | 更新日期: 2023-09-27 18:12:27

我有一个IntegerUpDown的实现:

<UserControl x:Class="Scoreboard.View.IntegerUpDown"
         ...
         xmlns:local="clr-namespace:Scoreboard.View"
         d:DesignHeight="28" Width="86"
         DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
    <StackPanel Orientation="Horizontal">
        <TextBox x:Name="TxtNum" x:FieldModifier="private" Margin="0,5,0,5" Width="50" 
                 Text="{Binding NumValue,Converter={local:NumberToStringConverter}, Mode=TwoWay}" PreviewTextInput="TxtNum_PreviewTextInput"/>
        <!-- Nothing interesting below -->
        <Button Margin="0,5" x:Name="CmdUp" x:FieldModifier="private" Width="18" Click="cmdUp_Click" >
            <Path Data="M 0,300 L 0,300 200,0 400,300" Stretch="Fill" Width="8.333" Height="5.833" Stroke="Black"/>
        </Button>
        <Button Margin="0,5" x:Name="CmdDown" x:FieldModifier="private" Width="18" Click="cmdDown_Click" >
            <Path Data="M 0,-300 L 0,-300 -200,0 -400,-300" Stretch="Fill" Width="8.333" Height="5.833" Stroke="Black"/>
        </Button>
    </StackPanel>
</Grid>

和后面的代码:

public partial class IntegerUpDown
{
    public int MaxValue { get; set; } = int.MaxValue;
    public int NumValue
    {
        get { return (int)GetValue(NumValueProperty); }
        set { SetValue(NumValueProperty, value); }
    }
    public static readonly DependencyProperty NumValueProperty =
        DependencyProperty.Register("NumValue",
        typeof(int), typeof(IntegerUpDown), new PropertyMetadata(0));

    public IntegerUpDown()
    {
        InitializeComponent();
    }
    //Nothing interesting below
    private void cmdUp_Click(object sender, RoutedEventArgs e)
    {
        if (NumValue < MaxValue)
        {
            NumValue++;
        }
    }
    private void cmdDown_Click(object sender, RoutedEventArgs e)
    {
        if (NumValue > 0)
        {
            NumValue--;
        }
    }
}

IntegerUpDown作为独立工作很好。其文本框中的文本与DP NumValue相同。问题是当这个控件被另一个使用,我想绑定NumValue与不同的属性。这样的

<UserControl x:Class="Scoreboard.View.TeamGameControl"
             ...
             d:DataContext="{d:DesignInstance ViewModel:ControlPanel}">
             <local:IntegerUpDown ... NumValue="{Binding Val, Mode=TwoWay}"/>
             <!--               doesn't do anything ↑ ... why? -->

属性Val在DataContext中的样子:

public class ControlPanel : INotifyPropertyChanged {
    public int Val
        {
            get { return _val; }
            set
            {
                _val = value;
                RaisePropertyChangedEvent("Val");
            }
        }
    }

两个属性(Val, NumValue)都没有更新。我做错了什么?

编辑:我已经把必要的部分剪掉了,这就是方案。

用户控件的依赖属性没有绑定到ViewModel的属性

请使用祖先等级2的relativesource

NumValue={Binding Value,Mode =TwoWay, 
          RelativeSource={RealtiveSource AncestorType =UserControl, AncestorLevel= 2}

否则你可以用ElementName代替RelativeSource

如果有帮助,请告诉我。

注意:我在IPhone上打字,请检查语法

多亏了slugster我修好了它。在IntegerUpDown中,我必须删除DataContext。然后像这样绑定文本框的文本

Text="{Binding NumValue,Converter={local:NumberToStringConverter}, Mode=TwoWay,
       RelativeSource={RelativeSource AncestorType={x:Type local:IntegerUpDown}}}"