【发布时间】:2013-11-16 18:54:43
【问题描述】:
我正在尝试在 WPF 中的第一个自定义用户控件上使用数据绑定。不幸的是,用户界面永远不会收到更新。
自定义用户控件的声明:
<views:EpisodeDetailsUserControl
x:Name="EpisodeDetailsUserControl"
DataContext="{Binding Episode}"/>
自定义用户控件 xaml (sn-p):
<Label
x:Name="TvShowNameLabel"
Grid.Row="0"
Grid.Column="1"
DataContext="{Binding TvShowName}" />
<Label
x:Name="SeasonIdLabel"
Grid.Row="1"
Grid.Column="1"
DataContext="{Binding SeasonId}" />
我希望在以下情况下更新用户控件:
private void DowloadedEpisodesListViewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var episode = DowloadedEpisodesListView.SelectedItem as IEpisode;
if (episode != null)
{
EpisodeDetailsUserControl.DataContext = episode; // <======
}
}
为了完整起见,请使用 IEpisode
public interface IEpisode
{
string FileName { get; }
string FullPath { get; }
string Directory { get; }
string Extension { get; }
string TvShowName { get; set; }
int SeasonId { get; set; }
int Id { get; set; }
string Name { get; }
bool IsValid { get; }
string NewFileName { get; }
string RenameProposal { get; }
}
但运气不好,什么也没有发生。
有人能指出正确的方向吗?
【问题讨论】:
-
实现IEEpisode的类是否也实现了INotifyPropertyChanged?
标签: c# wpf data-binding custom-controls