Bindinq XAML elemenst在stackpanel中对象(不是集合) [英] Bindinq XAML elemenst in stackpanel to an object (not a collection)
问题描述
我有以下课程
ProviderDataItem
SiteDataItem
然后我使用它们在我的SampleSiteDataSource中创建一个网站类
Then I use both to create a Sites class in my SampleSiteDataSource
public class Sites
{
private ProviderDataItem _provider = new ProviderDataItem();
public ProviderDataItem Provider
{
get { return this._provider; }
}
private ObservableCollection<SiteDataItem> _items= new ObservableCollection<SiteDataItem>();
public ObservableCollection<SiteDataItem> Items
{
get { return this._items; }
}
}
在我的页面加载状态下,我执行以下操作。我已经验证了GetProvider都返回了ProviderDateItem,并且GetSites返回了一个SiteBateItem的集合
。
protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
this.DefaultViewModel["Provider"] = SampleProviderDataSource.GetProvider("1");
this.DefaultViewModel["Sites"] = SampleSitesDataSource.GetSites();
}
然后在我的XAML中,我有以下标记。 d:DataContext正在指向Binding to Provider,数据显示在设计元素中。 我甚至可以通过更改传递给GetProviders
的参数来更改我访问的提供商,并在设计模式中显示正确的数据。 但是,当我运行应用程序时,StackPanel中没有显示任何内容。 我曾尝试使用< StackPanel.Resources>,< ControlPresenter>以及我在网上找到的其他一些建议。 但是没有什么
我尝试工作,其中大多数提供与此处发布的代码相同的结果。 这可能不难做到,我敢打赌,这只是一件非常简单的事情。我想错过。
Then in my XAML I have the following markup. The d:DataContext is working pointing Binding to Provider and the data shows in the design element. I can even change which provider I an accessing by changing the parameter passed to GetProviders and the correct data shows up in design mode. But, when I run the application nothing show up in the StackPanel. I have tried to use <StackPanel.Resources>, <ControlPresenter> and some other suggestions I found online. But nothing I try works and most of them deliver the same results as the code posted here. This cannot be that hard to do and I bet it is just something really simple I am missing.
<StackPanel x:Name="selectedProviderPanel" Grid.Column="0" Grid.Row="1" Margin="10,5,10,-9" Grid.RowSpan="2"
DataContext="Provider"
d:DataContext="{Binding Provider, Source={d:DesignInstance Type=data:SampleSitesDataSource, IsDesignTimeCreatable=True}}">
<TextBlock Text="{Binding Name}" Margin="10,0,10,20" Style="{StaticResource SubheaderTextStyle}" MaxHeight="60"/>
<Image Source="{Binding Image}" Height="254" Margin="10,0,10,20" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
<TextBlock Text="{Binding Url}" Margin="10,0,10,5" Style="{StaticResource BodyTextStyle}"/>
<TextBlock Text="{Binding SupportNumber}" Margin="10,0,10,5" Style="{StaticResource BodyTextStyle}"/>
</StackPanel>
我对XAML很陌生并且令人沮丧我无法弄清楚这一点。如果这是一个Windows或Web应用程序,我可以很容易地做到这一点。任何帮助将不胜感激,我已经浪费了4-6个小时试图通过试用
和错误来解决这个问题。
I am pretty new to XAML and it is getting frustrating I cannot figure this out. If this was a windows or web application I could do this really easy. Any help will be appreciated, I have wasted 4-6 hours trying to figure this out on my own through trial and error.
谢谢你
S. Shawn Mehaffie
Thanks
S. Shawn Mehaffie
;
推荐答案
要测试您的绑定尝试 此 。 DefaultViewModel [ " Provider" ]
= 新网站()。提供商;
To test your binding try this.DefaultViewModel["Provider"] = new Sites().Provider;
然后检查,但设置:
Then check but setting:
var provider = SampleProviderDataSource 。 GetProvider <跨度>(跨度> <跨度>" 1" 跨度> <跨度>); (检查提供商的价值)
然后 此 。 DefaultViewModel [ " Provider" ]
=提供商
var provider = SampleProviderDataSource.GetProvider("1"); (check value of provider)
and then this.DefaultViewModel["Provider"]
= provider
查看绑定是否正确,Name,Image,Url& SupportNumber是ProviderDataItem的所有属性吗? (不是字段,不是私有或受保护,而是在setter中实现INotifyPropertyChange PropertyChanged事件的公共属性)。
Looking at the binding is it correct that Name, Image, Url & SupportNumber are all properties of ProviderDataItem? (not fields, not private or protected but public properties that implement INotifyPropertyChange PropertyChanged event in the setter).
真的很遗憾微软已经创建了这些在培训人员时打破MVVM模式的模板模板新的XAML在坏习惯和使调试成为一场噩梦。大多数在XAML中工作的开发人员都会转储模板并绑定一个可测试的MVVM设计。
It is really unfortunate Microsoft has created these fudgy templates that break MVVM pattern as they train people new it XAML in bad habits and make debugging a nightmare. Most devs working in XAML dump the template and bind up a testable MVVM design.
这篇关于Bindinq XAML elemenst在stackpanel中对象(不是集合)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!