2012-02-26 104 views
1

我在使用我的父项目中的Listbox来显示在用户控件中定义的属性时遇到问题。更具体地说,我创建了一个包含webbrowsercontrol的用户控件,并且我定义了一个名为History的属性,它包含浏览会话期间访问过的URL的历史记录。我也有一个父项目,它链接到这个用户控件,我试图将History属性绑定到一个Listbox。重点是有人能够在此父项目中定义的Listbox中查看历史URL,其中历史URL通过绑定到用户控件的History属性来填充。如何将usercontrol属性绑定到Listbox

下面是我的代码概述什么,我试图做的:

用户控制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堆栈是前进和后退按钮实现,这是工作的罚款和_history的ObservableCollection包含来自会话网页浏览中显示的网址如下

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

这些似乎能正常工作,如I H大街实施了前进和后退按钮,他们工作正常。问题是我无法将FullWebBrowser.xaml.cs中定义的History属性正确绑定到包含Listbox的父项目。这显示如下

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是确定的。在这里,我试图实现一个Listbox,它显示带有时间戳的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)); 
     } 
    } 
} 

所以这是我的基本实现。无论我尝试什么,我都无法将Historypage中的Listbox绑定到项目外包含的FullWebBrowser用户控件的History属性,我已经使用解决方案资源管理器中的references选项引用了FullWebBrowser控件, Historypage.xaml.cs顶部,以及HistoryPage.xaml顶部的xmlns语句

任何有关如何完成这项任务的帮助将不胜感激!我一直在研究这个问题几个星期,无法在任何地方找到解决方案,甚至在其他人的帖子上徘徊。我必须尽快实施这个解决方案!感谢您提前提供的所有帮助。请包括代码来完成这一点,这将有助于看到这是如何实现以供将来参考!

+0

运行它时检查输出窗口,您是否看到一些绑定错误? – 2012-02-26 07:15:59

回答

0

ListBoxDataContextFullWebBrowser,因此您的结合应该是如下:

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

为了解决这类问题,自己尝试调试,断点那么你的代码检查中的各种元素的DataContext性质你的UI。

+0

谢谢你的回应! Paul,我没有看到任何数据绑定错误,而Colin,我已经按照您的建议设置了绑定,但是Listbox仍然没有填充History URL。什么是将Listbox的DataContext设置为FullWebBrowser的正确格式。对于HistoryPage。xaml.cs,我创建了一个新的FullWebBrowser实例,如下所示,因为我所有的研究表明,这是必要的更新列表框,因为新页面导航到UserControl '私人FullWebBrowser浏览器= new FullWebBrowser();' 什么是正确的数据绑定代码? – Matthew 2012-02-26 23:10:25

+0

我知道将一个ObservableCollection绑定到一个ListBox是很常见的,这是标准的,但是当将父项目之外的用户控件绑定到父项目中的Listbox时,是否存在我缺少的东西?我引用了外部用户控件,所以我在尝试绑定datacontext时没有检测到错误,但它并没有将用户控件的History属性URL从父项目的Listbox中填充。 – Matthew 2012-02-27 01:43:48

相关问题