【问题标题】:WPF - ComboBox not populating when inside Ribbon ControlWPF - 在功能区控件内部时不填充组合框
【发布时间】:2016-05-06 03:23:55
【问题描述】:

我正在使用 DevExpress WPF 控件套件。

我在窗口中添加了一个功能区控件,并在功能区控件中添加了两个组合框。它的代码如下。

MainView.xaml

<dxr:RibbonDefaultPageCategory>
 <dxr:RibbonPage Caption="Home">
 <dxr:RibbonPageGroup Caption="Operations">
   <dxb:BarButtonItem Content="Open" GlyphSize="Large" ItemClick="Open_ButtonClick"/>
      <dxb:BarButtonItem Content="Print" GlyphSize="Large" ItemClick="Print_ButtonClick"/>
 </dxr:RibbonPageGroup>

 <dxr:RibbonPageGroup Caption="Site">
     <dxb:BarStaticItem>
        <dxb:BarStaticItem.ContentTemplate>
           <DataTemplate>
               <dxe:ComboBoxEdit x:Name="siteComboBox" Width="150" Height="20" 
                    ItemsSource="{Binding Site}" SelectedItem="{Binding SelectedSite}"/>                                       
            </DataTemplate>
         </dxb:BarStaticItem.ContentTemplate>
       </dxb:BarStaticItem>
   </dxr:RibbonPageGroup>

   <dxr:RibbonPageGroup Caption="Plan Type">
       <dxb:BarStaticItem>
           <dxb:BarStaticItem.ContentTemplate>
               <DataTemplate> 
                   <dxe:ComboBoxEdit x:Name="planTypeComboBox" Width="150" Height="20"
                        MaxWidth="150" MaxHeight="100">
                      <dxe:ComboBoxEdit.Items >
                      <system:String>First</system:String>
                      <system:String>Second</system:String>
                      </dxe:ComboBoxEdit.Items>        
                   </dxe:ComboBoxEdit>
                </DataTemplate>
            </dxb:BarStaticItem.ContentTemplate>
        </dxb:BarStaticItem>
     </dxr:RibbonPageGroup>
   </dxr:RibbonPage>

MainViewModel.xaml.cs

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MainViewModel();
    }

MainViewModel.cs

 public class MainViewModel
{
    private IList<string> sites = new List<string>();
    private string selectedSite;

    private IList<string> planType = new List<string>();
    private string selectedPlanType; 

    /// <summary>
    /// Initializes a new instance of the <see cref="MainViewModel"/> class.
    /// </summary>
    public MainViewModel()
    {
        PopulateComboBoxes();
    }

    /// <summary>
    /// Populates the combo boxes.
    /// </summary>
    private void PopulateComboBoxes()
    {
        Site = new List<string>() {"First", "Second"};
    }

    public IList<string> Site
    {
        get
        {
            return sites;   
        }

        set
        {
            sites = value;
        }
    }

    public string SelectedSite
    {
        get
        {
            return selectedSite; 
        }

        set
        {
            selectedSite = value;
        }
    }
}

我注意到我创建了一个测试应用程序来使用ItemsSourceSelectedItem 填充组合框,它在测试应用程序中运行良好。但是,一旦我在功能区控件中实现相同的功能,组合框就不会填充。

如果我使用 &lt;system:String&gt; 对 ComboBox 项进行硬编码,它们似乎工作得很好。

谁能告诉我如何解决这个问题?

更新 - ANSWER:找到解决方法,组合框未填充的原因是

问题的原因是 BarStaticItem.Content 属性具有 Null 值。在这种情况下,位于 BarStaticItem 的内容模板中的 ComboBoxEdit 控件的数据上下文为空。

<dxr:RibbonControl DockPanel.Dock="Top" RibbonStyle="Office2010" Name="ribbon">
            <dxr:RibbonDefaultPageCategory>
                <dxr:RibbonPage Caption="Home">
                    <dxr:RibbonPageGroup Caption="Operations">
                        <dxb:BarButtonItem Content="Open" GlyphSize="Large" />
                        <dxb:BarButtonItem Content="Print" GlyphSize="Large" />
                    </dxr:RibbonPageGroup>

                    <dxr:RibbonPageGroup Caption="Site">
                        <dxb:BarStaticItem Content="{Binding}">
                            <dxb:BarStaticItem.ContentTemplate>
                                <DataTemplate>
                                    <dxe:ComboBoxEdit x:Name="siteComboBox" 
                                         Width="150" Height="20" 
                                         ItemsSource="{Binding Site}" 
                                         SelectedItem="{Binding SelectedSite}"/>
                                </DataTemplate>
                            </dxb:BarStaticItem.ContentTemplate>
                        </dxb:BarStaticItem>
                    </dxr:RibbonPageGroup>

                    <dxr:RibbonPageGroup Caption="Plan Type">
                        <dxb:BarStaticItem Content="{Binding}" 
                            IsEnabled="{Binding SelectedSite, 
                            Converter={dxmvvm:ObjectToBooleanConverter}}">
                            <dxb:BarStaticItem.ContentTemplate>
                                <DataTemplate>
                                    <dxe:ComboBoxEdit 
                                      x:Name="planTypeComboBox" 
                                      Width="150" Height="20"
                                      MaxWidth="150" MaxHeight="100"   
                                      ItemsSource="{Binding PlanType}"  
                                      SelectedItem="{Binding SelectedPlanType}">                                                                                        
                                    </dxe:ComboBoxEdit>
                                </DataTemplate>
                            </dxb:BarStaticItem.ContentTemplate>
                        </dxb:BarStaticItem>
                    </dxr:RibbonPageGroup>

                </dxr:RibbonPage>
            </dxr:RibbonDefaultPageCategory>
        </dxr:RibbonControl>     

【问题讨论】:

    标签: c# wpf xaml combobox devexpress


    【解决方案1】:

    是绑定错误,试试

    <dxe:ComboBoxEdit Width="150" 
                      Height="20" 
                      ItemsSource="{Binding Path=DataContext.Site,
                                            RelativeSource={RelativeSource 
                                            Mode=FindAncestor, 
                                            AncestorType={x:Type dxr:RibbonPageGroup}}}"/>
    

    或类似的东西。我现在无法复制完整的树。您可以打开Snoop 或新的Visual Studio 功能LiveVisualTree 并自行修复错误,也可以启用在输出窗口中显示绑定错误以备日后使用。

    【讨论】:

      【解决方案2】:

      问题的原因是BarStaticItem.Content 属性具有Null 值。在这种情况下,位于 BarStaticItem 的内容模板中的 ComboBoxEdit 控件的数据上下文为空。

      <dxr:RibbonControl DockPanel.Dock="Top" RibbonStyle="Office2010" Name="ribbon">
                  <dxr:RibbonDefaultPageCategory>
                      <dxr:RibbonPage Caption="Home">
                          <dxr:RibbonPageGroup Caption="Operations">
                              <dxb:BarButtonItem Content="Open" GlyphSize="Large" />
                              <dxb:BarButtonItem Content="Print" GlyphSize="Large" />
                          </dxr:RibbonPageGroup>
      
                          <dxr:RibbonPageGroup Caption="Site">
                              <dxb:BarStaticItem Content="{Binding}">
                                  <dxb:BarStaticItem.ContentTemplate>
                                      <DataTemplate>
                                          <dxe:ComboBoxEdit x:Name="siteComboBox" 
                                               Width="150" Height="20" 
                                               ItemsSource="{Binding Site}" 
                                               SelectedItem="{Binding SelectedSite}"/>
                                      </DataTemplate>
                                  </dxb:BarStaticItem.ContentTemplate>
                              </dxb:BarStaticItem>
                          </dxr:RibbonPageGroup>
      
                          <dxr:RibbonPageGroup Caption="Plan Type">
                              <dxb:BarStaticItem Content="{Binding}" 
                                  IsEnabled="{Binding SelectedSite, 
                                  Converter={dxmvvm:ObjectToBooleanConverter}}">
                                  <dxb:BarStaticItem.ContentTemplate>
                                      <DataTemplate>
                                          <dxe:ComboBoxEdit 
                                            x:Name="planTypeComboBox" 
                                            Width="150" Height="20"
                                            MaxWidth="150" MaxHeight="100"   
                                            ItemsSource="{Binding PlanType}"  
                                            SelectedItem="{Binding SelectedPlanType}">                                                                                        
                                          </dxe:ComboBoxEdit>
                                      </DataTemplate>
                                  </dxb:BarStaticItem.ContentTemplate>
                              </dxb:BarStaticItem>
                          </dxr:RibbonPageGroup>
      
                      </dxr:RibbonPage>
                  </dxr:RibbonDefaultPageCategory>
              </dxr:RibbonControl>     
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-14
        • 1970-01-01
        • 1970-01-01
        • 2011-11-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多