2011-09-11 9 views
0

リストボックスのデータソース (正確にはXmlDataProviderとDataTemplate)をプログラムで設定するユーザーコントロールがありますが、実行時に正しく表示されることはありません。ユーザーコントロールが読み込まれたとき。データプロバイダーの設定はすべて反映されていません。WPFで動作しないリストボックスのプログラムによるバインド

これを手伝ってもらえますか? 私は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です:

注意すべき10の

もの:

  1. CurrentUserにはUserDocプロパティ内のXML文書を含むオブジェクトです。
  2. 私がこのアプリケーションで行ったことの大部分は、私がグーグルで理解したものの小片であり、親切にも私に耐えます。

答えて

0

正直なところあなたのListBox.ItemsSource{Binding}すなわちバウンドとしてバインドされ

  1. ...私はあなたがWPF UIをロードするための新しいコーディング方法であれば理解することができませんが、いくつかのことは、ミスのように見えます全体のビューのDataContextに、あなたのコードの背後には、ViewBoxまたはその上位レベルの親のリストボックスのDataContextを設定していません。

  2. templateBindingtemplateBindingコードビハインドからの変数は、作成後どこでも使用されません。

  3. LoadContentsメソッドの最後に、FormDataProviderFormTemplateを再度設定するのはなぜですか? Resources辞書を「更新」したい場合は、正しい方法ではありません。リフレッシュするための

Resources削除し、あなたのResources辞書にリソースを追加すると同時に、DynamicResourceマークアップリファレンスを使用する必要があります。

あなたが達成したいと思っていることを教えてください。

+0

返信ありがとうございます。申し訳ありませんが、私のコーディングがむしろ面倒です。 私は最初にFormDataProviderのリソース値を次のようにコード化しました。 ((XmlDataProvider)Resources ["FormDataProvider"])Document = CurrentUser.UserDoc; しかし、これは機能しませんでした。 私は何が起こりたいのですか: 1。一度、ユーザーコントロールが読み込まれ、私はそれをフィードXMLストリームフィードに応じて、私はプログラムでそのXmlDocumentとそのXPathを定義することができるだろう(例:xmlが管理者からの場合、XPathは利用可能なAdminFormsノード) 2.結果をリストボックスにバインドし、選択したリスト項目をフレームに表示します。 ありがとう – chriscorn

関連する問題