簡體   English   中英

如何將用戶控件屬性綁定到列表框

[英]How to bind a usercontrol property to Listbox

我在上級項目中使用列表框來顯示用戶控件中定義的屬性時遇到問題。 更具體地說,我創建了一個包含webbrowsercontrol的用戶控件,並定義了一個名為History的屬性,該屬性包含在瀏覽會話期間訪問的URL的歷史記錄。 我還有一個父項目,該項目已鏈接到此用戶控件,在該項目中,我試圖將History屬性綁定到Listbox。 關鍵是使某人能夠在此父項目中定義的列表框中查看歷史記錄URL,通過綁定到用戶控件的History屬性來填充歷史記錄URL。

以下是我的代碼,概述了我要執行的操作:

用戶控件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包含來自Web瀏覽會話的URL,如下所示:

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

這些按鈕似乎工作正常,因為我已經實現了前進和后退按鈕,並且它們工作正常。 問題是我無法將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正常。 在這里,我嘗試實現一個列表框,該列表框顯示在其下方帶有時間戳的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屬性,但我已經在解決方案資源管理器中的using引用中使用了options引用了FullWebBrowser控件。在Historypage.xaml.cs的頂部,以及在HistoryPage.xaml頂部的xmlns語句

任何有關如何實現此目標的幫助將不勝感激! 我已經為此工作了兩周,甚至找不到其他人的帖子,卻找不到任何解決方案。 我必須盡快實施此解決方案! 感謝您的所有幫助。 請包括實現此目的的代碼,這將有很大幫助,以了解如何實現此功能以供將來參考!

您的ListBoxDataContextFullWebBrowser ,因此您的綁定應如下所示:

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

要自己解決此類問題,請嘗試調試,將代碼斷點,然后檢查UI中各種元素的DataContext屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM