将功能区组合框绑定到可观察集合

发布于 2024-12-14 05:50:20 字数 1757 浏览 4 评论 0原文

我有一个功能区组合框:

<r:RibbonComboBox DataContext="this"
                  SelectionBoxWidth="62"
                  VerticalAlignment="Center" 
                  IsEditable="True"  
                  Label="Saved Queries" 
                  Name="Saved_Queries"  
                  ToolTip="Select an item to run or edit" >
<r:RibbonGallery 
                  MaxColumnCount="1" 
                  Name="RibbonQu" 
                  ScrollViewer.VerticalScrollBarVisibility="Auto"
                  SelectionChanged="RibbonGallery_SelectionChanged" >
     <r:RibbonGalleryCategory ItemsSource="{Binding SavedXml}" >
            <r:RibbonGalleryItem Content="Green" Foreground="Green" />
            <r:RibbonGalleryItem Content="Blue" Foreground="Blue" />
            <r:RibbonGalleryItem Content="Orange" Foreground="Orange" />
     </r:RibbonGalleryCategory>
 </r:RibbonGallery>

我需要将 comobobox 的项目绑定到可观察的集合,如下所示:

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("QueryList.xml");

        XmlNodeList List = doc.SelectNodes("//Query");

        foreach (XmlElement element in List)
        {

            if (element == null) return;
            if (element != null)
            {
                //Saved_Queries.Items.Add(element.InnerText);

                _savedxml.Add(element.InnerText.ToString());  
            }
        }
    }
    public ObservableCollection<string> SavedXml
    {
        get { return _savedxml; }
    set{}
    }

但是当我运行它时,我在 comobox 中看不到任何内容。我认为问题出在数据上下文上,它已在代码中设置为其他内容,因此在组合框中我使用: 数据上下文=“这个” 但我仍然无法取得任何成就。我该怎么办?谢谢!

I have a ribbon combob box as:

<r:RibbonComboBox DataContext="this"
                  SelectionBoxWidth="62"
                  VerticalAlignment="Center" 
                  IsEditable="True"  
                  Label="Saved Queries" 
                  Name="Saved_Queries"  
                  ToolTip="Select an item to run or edit" >
<r:RibbonGallery 
                  MaxColumnCount="1" 
                  Name="RibbonQu" 
                  ScrollViewer.VerticalScrollBarVisibility="Auto"
                  SelectionChanged="RibbonGallery_SelectionChanged" >
     <r:RibbonGalleryCategory ItemsSource="{Binding SavedXml}" >
            <r:RibbonGalleryItem Content="Green" Foreground="Green" />
            <r:RibbonGalleryItem Content="Blue" Foreground="Blue" />
            <r:RibbonGalleryItem Content="Orange" Foreground="Orange" />
     </r:RibbonGalleryCategory>
 </r:RibbonGallery>

I need to bind the items of the comobobox to an observable collection as follows:

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("QueryList.xml");

        XmlNodeList List = doc.SelectNodes("//Query");

        foreach (XmlElement element in List)
        {

            if (element == null) return;
            if (element != null)
            {
                //Saved_Queries.Items.Add(element.InnerText);

                _savedxml.Add(element.InnerText.ToString());  
            }
        }
    }
    public ObservableCollection<string> SavedXml
    {
        get { return _savedxml; }
    set{}
    }

But i do not see anything in the comobox when i run it.I think the problem is with the data context, which has been set to others in the code, hence in the combobox i use :
DataContext="this"
but im still not able to achieve anything. how can i go about this? thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的那抹悲凉 2024-12-21 05:50:20

正如您所怀疑的,您错误地分配了 DataContext。

如果您需要组合仅在 SavedXml 中包含项目,而实际上不需要设置其 DataContext,请尝试删除 DataContext="this" 并添加 ItemsSource="{Binding SavedXml,relativeSource= {RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"

如果您需要设置 DataContext,请将分配更改为:DataContext="{BindingrelativeSource={RelativeSourceMode=FindAncestor, AncestorType={x:Type Window}}}",然后是 ItemsSource="{Binding SavedXml}"

在这两种情况下,由于 SavedXml 不是依赖项属性,并且它没有使用 INotifyProperty 更改的接口,因此您必须在 InitializeComponent() 之前填充项目。正在运行。事件更好:使 SavedXml 成为 DependencyProperty

As you suspected, you assign DataContext wrongly.

If you need the combo to just have the items in SavedXml, and not actually need to set its DataContext, try removing DataContext="this" and adding ItemsSource="{Binding SavedXml, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}".

If you do need to set the DataContext, change the assignment to: DataContext="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" and then ItemsSource="{Binding SavedXml}".

In both cases, since SavedXml is not a dependency property and it's not using INotifyProperty changed interface, you have to fill the items before InitializeComponent() is run. Event better: make SavedXml a DependencyProperty.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文