![](/img/trans.png)
[英]WPF MVVM Combobox Not Populating When Changing Selected Item On Another Control
[英]WPF - ComboBox not populating when inside Ribbon Control
我正在使用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
填充组合框,它在测试应用程序中运行良好。 但是,一旦我在功能区控件中实现了相同的功能,组合框就不会出现。
如果我使用<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
并LiveVisualTree
修复错误,还可以启用在输出窗口中显示绑定错误以备将来使用。
问题的原因是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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.