WPF - 在 Ribbon Control 中时 ComboBox 不填充
WPF - ComboBox not populating when inside Ribbon Control
我正在使用 DevExpress WPF 控件套件。
我在 window 中添加了一个功能区控件,并且在功能区控件中添加了两个组合框。它的代码如下。
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
填充组合框,它在测试应用程序中运行良好。但是,一旦我在功能区控件中实现了相同的功能,组合框就不会填充。
如果我使用 <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>
是因为绑定错误,试试
<dxe:ComboBoxEdit Width="150"
Height="20"
ItemsSource="{Binding Path=DataContext.Site,
RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType={x:Type dxr:RibbonPageGroup}}}"/>
或类似这样的东西。我现在无法重现整棵树。您可以打开 Snoop
或新的 Visual Studio
功能 LiveVisualTree
并自行修复错误,也可以启用在输出中显示绑定错误 window 以备将来使用。
问题的原因是 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>
我正在使用 DevExpress WPF 控件套件。
我在 window 中添加了一个功能区控件,并且在功能区控件中添加了两个组合框。它的代码如下。
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
填充组合框,它在测试应用程序中运行良好。但是,一旦我在功能区控件中实现了相同的功能,组合框就不会填充。
如果我使用 <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>
是因为绑定错误,试试
<dxe:ComboBoxEdit Width="150"
Height="20"
ItemsSource="{Binding Path=DataContext.Site,
RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType={x:Type dxr:RibbonPageGroup}}}"/>
或类似这样的东西。我现在无法重现整棵树。您可以打开 Snoop
或新的 Visual Studio
功能 LiveVisualTree
并自行修复错误,也可以启用在输出中显示绑定错误 window 以备将来使用。
问题的原因是 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>