简体   繁体   English

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

[英]Dependency property of user control doesn't bind to a property of ViewModel

I have an implementation of IntegerUpDown:我有一个 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>

and code behind:和后面的代码:

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 works fine as a standalone. IntegerUpDown 作为一个独立的工作正常。 Text in its TextBox is same as DP NumValue.其 TextBox 中的文本与 DP NumValue 相同。 The problem is when this control is used by another one and I want to bind NumValue with a different property.问题是当另一个控件使用此控件时,我想将 NumValue 与不同的属性绑定。 Like this像这样

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

And how looks property Val in DataContext:以及 DataContext 中的 Val 属性如何:

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

None of the two properties (Val, NumValue) is ever updated.这两个属性(Val、NumValue)都没有更新。 What am I doing wrong?我究竟做错了什么?

EDIT: I have cut out the necessary parts and here is the project .编辑:我已经剪掉了必要的部分,这里是项目

Please use relativesource with ancestor level 2请使用祖先级别 2 的相对源

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

Else you can use ElementName instead of RelativeSource否则你可以使用 ElementName 而不是 RelativeSource

Let me know if this helps.如果这有帮助,请告诉我。

Note:I am typing from IPhone..Please check the Syntax注意:我正在从 iPhone 打字..请检查语法

Thanks to slugster I managed to repair it.感谢 slugster 我设法修复了它。 In the IntegerUpDown I had to remove the DataContext.在 IntegerUpDown 中,我必须删除 DataContext。 And then bind the Text of the TextBox like this然后像这样绑定TextBox的Text

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM