2012-02-26 28 views
1

親コントロールのリストボックスを使用してユーザーコントロールで定義されたプロパティを表示する際に問題が発生します。具体的には、webbrowsercontrolを含むユーザーコントロールを作成しました。ブラウジングセッション中にアクセスしたURLの履歴を含むHistoryというプロパティを定義しました。また、このユーザーコントロールにリンクされている親プロジェクトがあり、履歴プロパティをリストボックスにバインドしようとしています。重要な点は、この親プロジェクトで定義されたリストボックス内の履歴URLを誰かが見ることができることです。ヒストリURLは、ユーザーコントロールのHistoryプロパティにバインドされます。以下はusercontrolプロパティをリストボックスにバインドする方法

私がやろうとしていますどのようなアウトラインに私のコードです:

ユーザーコントロールFullWebBrowser.xaml.cs

public partial class FullWebBrowser : UserControl 
{ 
    //ctr 
    public FullWebBrowser() 
    { 
     InitializeComponent(); 

     //for FullWebBrowser.xaml ContentGrid 
     ContentGrid.DataContext = this;    
    } 

    #region Fields 

    //The navigation urls of the browser. 
    private readonly Stack<Uri> _NavigatingUrls = new Stack<Uri>(); 

    //The history for the browser 
    private readonly ObservableCollection<string> _History = new ObservableCollection<string>(); 

    /// <summary> 
    /// Gets the History property for the browser. 
    /// </summary> 
    public ObservableCollection<string> History 
    { 
     get { return _History; } 

    } 

_NavigatingUrlsスタックが正常に動作している前後に、ボタンの実装のためのものです

//If the navigated uri is not in thehistory, add it 
      if (!_History.Contains(e.Uri.ToString())) 
       _History.Add(e.Uri.ToString()); 

これらは、I hと、正常に動作しているように見える次のように、と_HISTORYのObservableCollectionを示すwebbrowsingセッションからURLを含みますaveはフォワードボタンとバックボタンを実装しています。問題は、FullWebBrowser.xaml.csで定義されたHistoryプロパティをリストボックスを含む親プロジェクトに正しくバインドできないことです。

HistoryPage.xaml

xmlns:my="clr-namespace:FullBrowserControl;assembly=FullBrowserControl"> 

<!--LayoutRoot is the root grid where all page content is placed--> 
<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <!--Pivot Control--> 
    <controls:Pivot Title="QUEST"> 
     <!--Pivot item one--> 
     <controls:PivotItem Header="today"> 
      <Grid/> 
     </controls:PivotItem> 

     <!--Pivot item two--> 
     <controls:PivotItem Header="week"> 
      <Grid/> 
     </controls:PivotItem> 

     <!--Pivot item three--> 
     <controls:PivotItem Header="all"> 
      <StackPanel> 
       <ScrollViewer> 

        <ListBox x:Name="ListBox" ItemsSource="{Binding}" 
        SelectionChanged="ListBox_SelectionChanged"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel> 
          <TextBlock Text="{Binding Path=History}" Height="Auto"/> 
          <TextBlock Foreground="{StaticResource PhoneSubtleBrush}" 
             Text="{Binding Modified, Converter={StaticResource DateConverter}}" 
             Margin="24,0,0,12"/> 


         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

       </Scrollviewer> 
      </StackPanel> 

      <!--<Grid/>--> 
     </controls:PivotItem> 
    </controls:Pivot> 
</Grid> 

注意を次のようにこれはdateconverterはokです、示されています。ここで私はそれの下にタイムスタンプを持つURLを示すリストボックスを実装しようとしています。

Historypage.xaml.cs

public partial class HistoryPage : PhoneApplicationPage 
{ 
    //Temporary State 
    public static readonly Setting<int> CurrentHistoryIndex = new Setting<int>("CurrentHistoryIndex", -1); 

    private FullWebBrowser browser = new FullWebBrowser(); 

    public HistoryPage() 
    { 
     InitializeComponent(); 

     //create new instance of FullWebBrowser user control? 
     this.browser = new FullWebBrowser(); 
     this.DataContext = browser; 
     //browser.DataContext = this; 
     //this.DataContext = browser.History; 
    } 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     base.OnNavigatedTo(e); 

     //Clear the selection so selecting the same item twice in a row will still raise the SelectedChanged event 
     CurrentHistoryIndex.Value = -1; 
     this.ListBox.SelectedIndex = -1; 
    } 

    void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     if (ListBox.SelectedIndex >= 0) 
     { 
      //navigate to the page containing the user control for the selected item 
      //how to navigate to Mainpage.xaml and load webbrowsercontrol with selected url?? 
      CurrentHistoryIndex.Value = ListBox.SelectedIndex; 
      this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); 
     } 
    } 
} 

は、次のように この親プロジェクトのページのための背後にあるコードがあるので、これが私の基本的な実装です。私は何をしようと試みても、リストボックスをヒストリページの履歴プロパティにバインドすることはできません。プロジェクトの外部にあるFullWebBrowserユーザーコントロールのソリューションプロパティで、ソリューションエクスプローラの参照オプションを使用してFullWebBrowserコントロールを参照しました。 Historypage.xaml.csの先頭、HistoryPage.xamlの先頭のxmlnsステートメント

これがどのように達成されるかについてのご支援をいただければ幸いです。私はこれを数週間働いていて、他の投稿を見回してもどこでも解決策を見つけることができません。私はすぐにこのソリューションを実装する必要があります!すべてのあなたの助けをありがとう。これを達成するためのコードを含めてください。将来の参照のためにこれがどのように実装されているかを知るのに大いに役立ちます!

+0

実行時に出力ウィンドウを確認します。バインディングエラーが表示されますか? –

答えて

0

あなたListBoxDataContextは次のようにので、あなたが結合する必要があり、FullWebBrowserです:

<ListBox x:Name="ListBox" ItemsSource="{Binding Path=History}"/> 

あなたのコードをブレークポイントを設定、自分でデバッグをしようとこの種の問題を解決するために、その後の様々な要素のDataContext性質を調べますあなたのUI。

+0

ありがとうございました!ポール、私はデータバインディングのエラーは表示されません、そしてコリン、私はあなたが提案したように自分のバインディングを設定しましたが、リストボックスはまだヒストリのURLを入力していません。リストボックスのDataContextをFullWebBrowserに設定するための正しい形式は何でしょうか。 HistoryPageのために。xaml.cs、次のようにFullWebBrowserの新しいインスタンスを作成しました。私の研究では、新しいページがUserControlでナビゲートされるときにリストボックスを更新する必要があることが示されているからです。 'private FullWebBrowser browser = new FullWebBrowser(); 正しいデータバインディングコードは何ですか? – Matthew

+0

ObservableCollectionをリストボックスにバインドするのが一般的だとわかっていますが、これは標準ですが、親プロジェクトの外にあるユーザーコントロールを親プロジェクトのリストボックスにバインドすると、私はdatacontextをバインドしようとするとエラーを検出していないので、外部のusercontrolを参照しましたが、ヒストリプロパティのurlをユーザーコントロールから親プロジェクトのリストボックスに単に挿入していません。 – Matthew

関連する問題