我有一个问题绑定UserControl中的依赖项属性。初始化它时会得到一个值,但不会更新。我可能错过了一些显而易见的,这里有一些代码段:WPF绑定依赖属性
这是我绑定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);}
}
这里是所选择的用户进行更新列表中,这是一个使用用户控件一个观点:
<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
。任何帮助赞赏。
感谢您的回复。在我详细阅读整篇文章之前,请快速发表评论。 ViewModel中的所选用户正在更新,因为这是我认为的第一件事情。我也有一个TextBox,它是窗口的成员,它使用与依赖项属性上使用的绑定相同的绑定来更新余额。该值初始化正常,但后续更新控件的TextBox中的值保持不变,因此我认为您的最后一点值得进一步调查 – paj7777
因此,即使XAML抱怨无法解析Balance,仍然将SelectedUser的源更改为ListView ,我猜在设计时它显然会做。 – paj7777