![](/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.