【问题标题】:Dependency property of user control doesn't bind to a property of ViewModel用户控件的依赖属性未绑定到 ViewModel 的属性
【发布时间】:2016-10-10 16:38:13
【问题描述】:

我有一个 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 作为独立的工作正常。其 TextBox 中的文本与 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? -->

以及 DataContext 中的属性 Val 的外观:

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

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

编辑: 我已经剪掉了必要的部分,这里是project

【问题讨论】:

  • if (!int.TryParse(TxtNum.Text, out temp)) - 删除不相等并且在绑定中也将值更改为 Val,例如 NumValue="{Binding Val, Mode=TwoWay}"
  • 我在写这个问题时犯了一个错误。现在它被编辑了。不相等并不能解决我的问题。
  • 您是否更改了绑定?
  • 我已经编辑了绑定中名称不一致的错误。
  • 在您的 UserControl 上删除 DataContext="{Binding RelativeSource={RelativeSource Self}}"

标签: c# wpf xaml binding


【解决方案1】:

请使用祖先级别 2 的 relativesource

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

否则你可以使用 ElementName 而不是 RelativeSource

如果这有帮助,请告诉我。

注意:我是从 iPhone 输入的。请检查语法

【讨论】:

  • 不行,还是不行。 Ancestor Type = UserControl 不是很奇怪吗?不应该有别的东西来代替 UserControl 吗?我尝试更改它,但它仍然无法正常工作。而且我不能使用 ElementName(我认为),因为 ControlPanel 不是控件(是的,名字不好)。
  • 你试过 AncestorLevel 吗?也试试 x:type UserControl
  • 我尝试使用 Ancestors 但它没有帮助,因为我想将它绑定到 ViewModel 并且它不是直接祖先。该用户控件在视图中。
  • 不要使用AncestorLevel,使用AncestorType={x:Type IntegerUpDown}
  • Slugster,你是我的白马骑士。谢谢。
【解决方案2】:

感谢 slugster,我设法修复了它。 在 IntegerUpDown 中,我必须删除 DataContext。然后像这样绑定TextBox的Text

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    相关资源
    最近更新 更多