的屬性我有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。
的文本,如果(int.TryParse(TxtNum.Text,出臨時)!) - 刪除不等於也是在綁定值更改爲瓦爾像NumValue =「{結合Val,Mode = TwoWay}「 –
我在寫這個問題時犯了一個錯誤。現在它被編輯了。不相等並不能解決我的問題。 – Korhak
你改變了你的約束力嗎? –