我正在使用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;
}
}
}
我注意到我创建了一个测试应用程序,使用ItemsSource
和SelectedItem
填充组合框,它在测试应用程序中运行良好。但是,一旦我在Ribbon Control中实现了相同的功能,组合框就不会填充。
如果我使用<system:String>
对ComboBox项进行硬编码,它们似乎工作正常。
有谁能告诉我如何解决这个问题?
更新 - 答案:找到解决办法并且没有填充组合框的原因是
问题的原因是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>
答案 0 :(得分:0)
由于绑定错误,请尝试
<dxe:ComboBoxEdit Width="150"
Height="20"
ItemsSource="{Binding Path=DataContext.Site,
RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType={x:Type dxr:RibbonPageGroup}}}"/>
或类似的东西。我现在无法重现完整的树。您可以打开Snoop
或新的Visual Studio
功能LiveVisualTree
并自行修复错误,也可以在输出窗口中启用显示绑定错误,以备将来准备。
答案 1 :(得分:0)
问题的原因是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>