2016-10-10 46 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

相關問題