繁体   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