无法绑定到视图模型
本文关键字:视图 模型 绑定 | 更新日期: 2023-09-27 18:32:01
我有一个问题,在过去的几个小时里,这是ViewModel代码:(PS:我不能分享url流,但不要担心它的行进,因为我用断点测试了它)
private ObservableCollection<CustomerPublic> customers;
List<CustomerPublic> liste = new List<CustomerPublic>();
public ObservableCollection<CustomerPublic> Customers
{
get
{ return customers; }
set
{
if (customers != value)
{
customers = value;
RaisePropertyChanged("Customers");
}
}
}
private int id;
public int ID
{
get
{
return id;
}
set
{
id = value;
RaisePropertyChanged("ID");
}
}
public Detail_AgenceViewModel(int id)
{
this.ID = id;
PopulateCollection();
}
public Detail_AgenceViewModel()
{
}
private void PopulateCollection()
{
ParseFeedRequest();
}
private void ParseFeedRequest()
{
RestClient client = new RestClient();
client.BaseUrl = "....";
RestRequest request = new RestRequest();
.......
client.ExecuteAsync(request, ParseFeedCallBack);
}
public void ParseFeedCallBack(IRestResponse response)
{
if (response.StatusCode == HttpStatusCode.OK)
{
ParseXMLFeed(response.Content);
}
}
private void ParseXMLFeed(string feed)
{
if (feed == null)
return;
XElement xmlItems = XElement.Parse(feed);
liste = (from response in xmlItems.Descendants("result")
let lib = response.Element("lib")
let adresse = response.Element("adresse")
select new CustomerPublic
{
lib = lib == null ? null : lib.Value,
adresse = adresse == null ? null : adresse.Value,
}).ToList();
Customers = new ObservableCollection<CustomerPublic>(liste);
}
视图:
<phone:PhoneApplicationPage.DataContext>
<vm:Detail_AgenceViewModel/>
</phone:PhoneApplicationPage.DataContext>
<Grid x:Name="LayoutRoot"
Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel"
Grid.Row="0"
Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle"
Text="MY APPLICATION"
Style="{StaticResource PhoneTextNormalStyle}" />
<TextBlock x:Name="PageTitle"
Text="page name"
Margin="9,-7,0,0"
Style="{StaticResource PhoneTextTitle1Style}" />
</StackPanel>
<!--ContentPanel - place additional content here-->
<StackPanel x:Name="ContentPanel" Grid.Row="2" Margin="12,0,12,0" Orientation="Vertical">
<!--TextBox Text="{Binding Count, Mode=TwoWay}" x:Name="tbCount" />
<TextBlock Text="{Binding Count}" /-->
<ListBox x:Name="Agences" ItemsSource="{Binding Customers}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding lib}" />
<TextBlock Text="{Binding adresse}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
问题是一切顺利 顾客即使她都装了,但什么也没有出现! 有人有想法吗?
您正在将客户设置为可观察集合的新实例!
将可观察集合更改为新实例时,必须使用 INotifyPropertyChanged 告知视图集合已更改为新实例 - 尽管会通知对集合中项的更改,但不会通知对集合本身的更改。
执行此操作时:
Customers = new ObservableCollection<CustomerPublic>(liste);
视图仍绑定到 OLD 集合。你应该做:
Customers.Clear();
foreach(var item in liste)
Customers.Add(item);
或者确保 Customers 属性调用 NotifyPropertyChanged 函数。
查看此视频或文章以获取更多信息:
http://www.codeproject.com/Articles/371217/Apex-Part-1-Create-Your-First-MVVM-Applicationhttp://www.youtube.com/watch?v=m4cx9w5fiwk&feature=youtu.be
祝你好运,如果有帮助,请告诉我!!
我有类似的问题;
public void FillList(List<StockItem> siList)
{
listBox.ItemsSource = siList;
}
其中 sIList 是 X 项的填充列表,具有正确命名的属性。程序构建并运行良好,但未显示列表框。(此问题在过渡到 MVVM 时开始)
我知道了。
检查您的数据上下文 - 我敢打赌它是空的。我在WP7中遇到了完全相同的问题。在PhoneApplicationPage的构造函数中,执行以下操作:
数据上下文 = 新Detail_AgenceViewModel();
并在那里初始化它。在 WP7 中,当我在 XAML 中创建数据上下文时,它是空的。这有帮助吗?