如何将usercontrol属性绑定到Listbox

时间:2012-02-26 03:26:13

标签: c# wpf windows-phone-7

我在父项目中使用Listbox来显示用户控件中定义的属性时遇到问题。更具体地说,我创建了一个包含webbrowsercontrol的用户控件,并且我定义了一个名为History的属性,其中包含浏览会话期间访问的URL的历史记录。我还有一个父项目,它链接到这个用户控件,我试图将History属性绑定到Listbox。关键是有人能够在此父项目中定义的列表框中查看历史记录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包含webbrowsing session中的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属性正确绑定到包含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语句

任何有关如何实现这一目标的帮助将不胜感激!我已经开展了几个星期的工作,无法在任何地方找到解决方案,甚至在其他人的帖子上徘徊。我必须尽快实施这个解决方案!感谢您的所有帮助。请包含完成此操作的代码,这将有助于了解如何实现此功能以供将来参考!

1 个答案:

答案 0 :(得分:0)

DataContext的{​​{1}}为ListBox,因此您的约束应如下所示:

FullWebBrowser

要解决此类问题,请尝试调试,断开代码,然后检查UI中各种元素的<ListBox x:Name="ListBox" ItemsSource="{Binding Path=History}"/> 属性。