2016-10-10 43 views
-1

的属性我有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? --> 

怎么看起来财产的Val的DataContext:

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

两个属性(VAL,NumValue)都不是不断更新的。我究竟做错了什么?

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

+0

的文本,如果(int.TryParse(TxtNum.Text,出临时)!) - 删除不等于也是在绑定值更改为瓦尔像NumValue =“{结合Val,Mode = TwoWay}“ –

+0

我在写这个问题时犯了一个错误。现在它被编辑了。不相等并不能解决我的问题。 – Korhak

+0

你改变了你的约束力吗? –

回答

0

感谢slugster我设法修复它。 在IntegerUpDown我不得不删除DataContext。然后绑定文本框的这样

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

请使用带的RelativeSource始祖级2

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

;否则可以使用的ElementName代替的RelativeSource

让我知道,如果这有助于。

注:我从IPhone..Please键入检查语法

+0

不,仍然不起作用。是不是祖先类型=用户控制奇怪?应该没有别的而不是UserControl?我试图改变它,但它仍然无法正常工作。 而我不能使用ElementName(我认为),因为ControlPanel不是一个控件(是的,坏名字)。 – Korhak

+0

你有没有尝试过AncestorLevel?也请尝试x:type UserControl –

+0

我试图和祖先一起玩,但它没有帮助,因为我想将它绑定到ViewModel,它不是一个直接的祖先。该用户控件处于查看状态。 – Korhak

相关问题