我在使用我的父項目中的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語句
任何有關如何完成這項任務的幫助將不勝感激!我一直在研究這個問題幾個星期,無法在任何地方找到解決方案,甚至在其他人的帖子上徘徊。我必須儘快實施這個解決方案!感謝您提前提供的所有幫助。請包括代碼來完成這一點,這將有助於看到這是如何實現以供將來參考!
運行它時檢查輸出窗口,您是否看到一些綁定錯誤? – 2012-02-26 07:15:59