我遇到了一个在UserControl中绑定依赖属性的问题。当它初始化时,它会得到一个值,但是之后它不会更新。 我可能错过了一些明显的东西,以下是一些代码片段:
这是我绑定BalanceContent
依赖属性的地方:
<Game:PlayerDetails x:Name="SelectedPlayerDetails" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Grid.RowSpan="4"
BalanceContent="{Binding Source={StaticResource UserData}, Path=SelectedUser.Balance, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
</Game:PlayerDetails>
这是在UserControl
中的TextBox
:
<TextBox VerticalAlignment="Center" FontFamily="Formata" FontSize="20" Grid.Column="2"
Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}, Path=BalanceContent}"
Grid.Row="7"></TextBox>
以下是依赖属性:
public static readonly DependencyProperty BalanceContentProperty = DependencyProperty.Register(
"BalanceContent", typeof(string), typeof(PlayerDetails));
public string BalanceContent
{
get
{return (string) GetValue(BalanceContentProperty);}
set
{SetValue(BalanceContentProperty, value);}
}
这是一个使用UserControl的视图,其中包含被更新的选定用户列表:
<ListView x:Name="lstAccounts" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Grid.RowSpan="4"
ItemsSource="{Binding Source={StaticResource UserData}, Path=CurrentUserSearch}"
SelectedItem="{Binding Source={StaticResource UserData}, Path=SelectedUser}"
而 SelectedUser
是在实现了 INotifyPropertyChanged
接口的类中定义的:
public User SelectedUser
{
get
{
return _selectedUser;
}
set
{
_selectedUser = value;
OnPropertyChanged(new PropertyChangedEventArgs("SelectedUser"));
}
}
这个想法是,当在列表中选择一个新的用户时,TextBox应该更新,但目前它没有更新。我已经将绑定放在本地的TextBox上,并且可以正常更新,但不能在DependencyProperty
上更新。感谢任何帮助。