我有一个用户控件,可以通过编程方式设置列表框的数据源 (确切地说是XmlDataProvider和DataTemplate)但在运行时它从未正确显示。加载用户控件时。数据提供者的所有设置都没有反映出来。
你可以帮我这个吗? 我是开发WPF应用程序的新手。TIA
以下是代码:
XAML
<UserControl x:Class="ENGAGIAUCL.Views.ImageViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="500" Width="550">
<UserControl.Resources>
<XmlDataProvider x:Key="FormDataProvider"/>
<DataTemplate x:Key="FormTemplate">
<Border Background="#2200FF00"
BorderBrush="Black"
BorderThickness="1"
CornerRadius="8"
Margin="2,4,2,4"
Padding="4">
<StackPanel HorizontalAlignment="Stretch">
<TextBlock Text="{Binding XPath=name}" />
</StackPanel>
</Border>
</DataTemplate>
</UserControl.Resources>
<DockPanel>
<Border DockPanel.Dock="Top"
Height="45">
<TextBlock x:Name="tbkContentTitle"
Text="Content Title Goes Here"
VerticalAlignment="Center"
HorizontalAlignment="Center"
FontSize="20">
</TextBlock>
</Border>
<DockPanel>
<Border DockPanel.Dock="Bottom">
</Border>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListBox x:Name="lbPreview"
IsSynchronizedWithCurrentItem="True"
VerticalAlignment="Top"
Height="455"
Grid.Column="0"
ItemsSource="{Binding}">
</ListBox>
<Frame x:Name="ActualContentFrame"
Grid.Column="1"
Source="{Binding XPath=url}">
</Frame>
</Grid>
</DockPanel>
</DockPanel>
</UserControl>
这是.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using EngagiaCL.CommonObjects;
using EngagiaCL.Functions;
namespace ENGAGIAUCL.Views
{
/// <summary>
/// Interaction logic for ImageViewer.xaml
/// </summary>
public partial class ImageViewer : UserControl
{
public ImageViewer()
{
InitializeComponent();
Loaded += (s, e) =>
{
LoadContents();
};
}
#region Methods
private void LoadContents()
{
if (CurrentUser != null)
{
XmlDataProvider provider = (XmlDataProvider)this.FindResource("FormDataProvider");
DataTemplate template = (DataTemplate)this.FindResource("FormTemplate");
Binding templatebinding = new Binding();
provider.Document = CurrentUser.UserDoc;
provider.XPath = GetResourcePath();
template.DataType = (object)GetDataTemplateObject();
Resources["FormDataProvider"] = provider;
Resources["FormTemplate"] = template;
}
}
private string GetResourcePath()
{
string path = string.Empty;
if (ContentType == "ADMIN")
{
path = "/SyncLoginResponse/AdminForms/AdminForm";
}
else
{
path = "/SyncLoginResponse/Forms/Form";
}
return path;
}
private string GetDataTemplateObject()
{
string templateobject = string.Empty;
if (ContentType == "ADMIN")
{
templateobject = "AdminForm";
}
else
{
templateobject = "Form";
}
return templateobject;
}
#endregion
#region Properties
public UserInformation CurrentUser { get; set; }
public string ContentType { get; set; }
#endregion
}
}
这是xml供参考:
</SyncLoginResponse>
<AdminForms>
<AdminForm>
<name>Best Form Ever/html</name>
<url>
http://blahblahblah/
</url>
</AdminForm>
</AdminForms>
</SyncLoginResponse>
注意事项:
答案 0 :(得分:0)
老实说,我无法理解你的是否是一个加载WPF UI的新编码实践,但很少有东西看起来像是错过......
您的ListBox.ItemsSource
绑定为{Binding}
,即绑定到整个视图的DataContext
,但在您的代码中没有任何地方,您是否设置了DataContext
视图或列表框中的任何更高级别的父级。
创建后,您的templateBinding
变量不会在任何地方使用。
为什么在FormDataProvider
方法结束时再次设置FormTemplate
和LoadContents
?如果您想要“刷新”Resources
词典,那么这不是正确的方法。
要刷新Resources
,您需要在Resources
字典中删除和添加资源,同时使用DynamicResource
标记参考。
让我知道你想要实现的是什么。