2016-10-10 9 views
-1

のプロパティにバインドされていません。依存関係プロパティは、私がIntegerUpDownの実装を持っているViewModelに

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として正常に動作します:

<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> 

と背後にあるコードスタンドアロン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プロパティにヴァルに見える2つのプロパティ(ヴァル、NumValue)の

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

どれがこれまでに更新されません。私は間違って何をしていますか?

編集: 必要な部分を削除しました。ここではprojectです。

+0

のようなテキストボックスのテキストをバインドVal、Mode = TwoWay} " –

+0

私はこの質問を書いていたときに間違いを犯しました。今すぐ編集されます。しかし、私の問題は解決しません。 – Korhak

+0

バインディングを変更しましたか? –

答えて

0

感謝をチェックIPhone..Pleaseから入力しています。 IntegerUpDownでは、DataContextを削除する必要がありました。 (!int.TryParse(TxtNum.Text、アウト一時は)) - {バインディングNumValue = "などのヴァルへの結合の変化値が等しくないとも外した場合、その後、この

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

あなたはこのことができますならば、私に教えてください代わりにRelativeSource

ののElementNameを使用することができそうで先祖レベル2

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

でrelativesourceを使用してください。

注:私は、私はそれを修復するために管理slugsterする構文

+0

いいえ、まだ動作しません。 Ancestor Type = UserControlではありませんか? UserControlの代わりに何か他のものがありますか?私はそれを変更しようとしましたが、それでも動作しません。 ControlPanelがコントロール(ええ、悪い名前)なので、私はElementNameを使うことができません。 – Korhak

+0

AncestorLevelで試しましたか?またxを試してみてください:UserControlと入力してください –

+0

私はAncestorsと一緒に遊んだりしようとしましたが、ViewModelにバインドしたいので、助けにならない、直接の祖先ではありません。そのユーザーコントロールが表示されます。 – Korhak

関連する問題