2011-10-17 83 views
0

設置SelectedIndex屬性的組合框這聽上去也許微不足道,但我有與組合框設置所選項目的問題;)如何的DataTemplate

我想才達到什麼:

我想有頁面加載後選擇列表的第一個元素。

XAML代碼:

   <DataTemplate> 
            <ComboBox x:Name="DeviceComboBox" SelectedIndex="1" SelectionChanged="DeviceComboBox_SelectionChanged"> 
             <ComboBox.Items> 
              <ComboBoxItem x:Name="Switch" Content="Switche"/> 
              <ComboBoxItem x:Name="Firewall" Content="Firewalle"/> 
              <ComboBoxItem x:Name="Host" Content="Hosty" /> 
              <ComboBoxItem x:Name="SRF1" Content="SRF1"/> 
             </ComboBox.Items> 
            </ComboBox> 
           </DataTemplate> 

結果: AG_E_UKNOWN_ERROR [行:49的位置:55] 49 行:

</ComboBox> 

錯誤詳細信息

 
    w MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData) 
    w MS.Internal.XcpImports.MethodEx(DependencyObject obj, String name) 
    w MS.Internal.XcpImports.DataTemplate_LoadContent(DataTemplate template) 
    w System.Windows.Controls.DataGridTemplateColumn.GenerateElement(DataGridCell cell, Object dataItem) 
    w System.Windows.Controls.DataGrid.PopulateCellContent(Boolean isCellEdited, DataGridColumn dataGridColumn, DataGridRow dataGridRow, DataGridCell dataGridCell) 
    w System.Windows.Controls.DataGrid.AddNewCellPrivate(DataGridRow row, DataGridColumn column) 
    w System.Windows.Controls.DataGrid.CompleteCellsCollection(DataGridRow dataGridRow) 
    w System.Windows.Controls.DataGrid.GenerateRow(Int32 rowIndex, Int32 slot, Object dataContext) 
    w System.Windows.Controls.DataGrid.InsertElementAt(Int32 slot, Int32 rowIndex, Object item, DataGridRowGroupInfo groupInfo, Boolean isCollapsed) 
    w System.Windows.Controls.DataGrid.InsertRowAt(Int32 rowIndex) 
    w System.Windows.Controls.DataGridDataConnection.NotifyingDataSource_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) 
    w System.Windows.Data.PagedCollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) 
    w System.Windows.Data.PagedCollectionView.ProcessAddEvent(Object addedItem, Int32 addIndex) 
    w System.Windows.Data.PagedCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args) 
    w System.Windows.Data.PagedCollectionView.<.ctor>b__0(Object sender, NotifyCollectionChangedEventArgs args) 
    w System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) 
    w System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item) 
    w System.Collections.ObjectModel.Collection`1.Add(T item) 
    w LANOS.Views.Customers.onCustomerListLoaded(LoadOperation`1 loadOper) 
    w System.ServiceModel.DomainServices.Client.DomainContext.<>c__DisplayClass13`1.<Load>b__11(LoadOperation lo) 
    w System.ServiceModel.DomainServices.Client.LoadOperation.<>c__DisplayClass4`1.<Create>b__0(LoadOperation`1 arg) 
    w System.ServiceModel.DomainServices.Client.LoadOperation`1.InvokeCompleteAction() 
    w System.ServiceModel.DomainServices.Client.OperationBase.Complete(Object result) 
    w System.ServiceModel.DomainServices.Client.LoadOperation.Complete(DomainClientResult result) 
    w System.ServiceModel.DomainServices.Client.DomainContext.CompleteLoad(IAsyncResult asyncResult) 
    w System.ServiceModel.DomainServices.Client.DomainContext.<>c__DisplayClass1b.<Load>b__17(Object) 

Caused by: AG_E_UNKNOWN_ERROR [Line: 49 Position: 55] 

    w MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData) 
    w MS.Internal.XcpImports.MethodEx(DependencyObject obj, String name) 
    w MS.Internal.XcpImports.DataTemplate_LoadContent(DataTemplate template) 
    w System.Windows.Controls.DataGridTemplateColumn.GenerateElement(DataGridCell cell, Object dataItem) 
    w System.Windows.Controls.DataGrid.PopulateCellContent(Boolean isCellEdited, DataGridColumn dataGridColumn, DataGridRow dataGridRow, DataGridCell dataGridCell) 
    w System.Windows.Controls.DataGrid.AddNewCellPrivate(DataGridRow row, DataGridColumn column) 
    w System.Windows.Controls.DataGrid.CompleteCellsCollection(DataGridRow dataGridRow) 
    w System.Windows.Controls.DataGrid.GenerateRow(Int32 rowIndex, Int32 slot, Object dataContext) 
    w System.Windows.Controls.DataGrid.InsertElementAt(Int32 slot, Int32 rowIndex, Object item, DataGridRowGroupInfo groupInfo, Boolean isCollapsed) 
    w System.Windows.Controls.DataGrid.InsertRowAt(Int32 rowIndex) 
    w System.Windows.Controls.DataGridDataConnection.NotifyingDataSource_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) 
    w System.Windows.Data.PagedCollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) 
    w System.Windows.Data.PagedCollectionView.ProcessAddEvent(Object addedItem, Int32 addIndex) 
    w System.Windows.Data.PagedCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args) 
    w System.Windows.Data.PagedCollectionView.<.ctor>b__0(Object sender, NotifyCollectionChangedEventArgs args) 
    w System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) 
    w System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, T item) 
    w System.Collections.ObjectModel.Collection`1.Add(T item) 
    w LANOS.Views.Customers.onCustomerListLoaded(LoadOperation`1 loadOper) 
    w System.ServiceModel.DomainServices.Client.DomainContext.<>c__DisplayClass13`1.<Load>b__11(LoadOperation lo) 
    w System.ServiceModel.DomainServices.Client.LoadOperation.<>c__DisplayClass4`1.<Create>b__0(LoadOperation`1 arg) 
    w System.ServiceModel.DomainServices.Client.LoadOperation`1.InvokeCompleteAction() 
    w System.ServiceModel.DomainServices.Client.OperationBase.Complete(Object result) 
    w System.ServiceModel.DomainServices.Client.LoadOperation.Complete(DomainClientResult result) 
    w System.ServiceModel.DomainServices.Client.DomainContext.CompleteLoad(IAsyncResult asyncResult) 
    w System.ServiceModel.DomainServices.Client.DomainContext.<>c__DisplayClass1b.<Load>b__17(Object) 

任何想法?

+0

可能的問題是添加項目之前選定的指標設置?您可以嘗試在加載窗口或控件後設置索引。除此之外:SelectionChanged方法正在做什麼? – MatthiasG

+0

選擇變更可以簡單地重定向到另一個網頁。沒有花哨,沒有SelectedIndex屬性工作正常。嗯,我怎麼可以從後面的頁面代碼訪問DataTemplate對象屬性? 看起來像這樣.DeviceComboBox不起作用。 – neurotix

+1

如果你想創建一個具有特殊行爲的組合框,我會建議創建一個從ComboBox派生並實現你的行爲的新類。這應該做的工作,你不需要數據模板。另一種方法是創建一個行爲元素。 – MatthiasG

回答

1

正如我在你的評論讀你想使用一個組合框,當它被加載時它會自動有一個選擇。因此,我建議兩種可能性:編寫控制從組合框獲得或添加行爲爲默認組合框。

從組合框派生:

public class MyComboBox : ComboBox 
{ 
    public MyComboBox() 
    { 
    Loaded += ComboBoxLoaded; 
    } 

    private void ComboBoxLoaded(object sender, RoutedEventArgs e) 
    { 
    if(Count > 1) 
    { 
     SelectedIndex = 1; 
    } 
    } 
} 

用法:

<MyComboBox> 
    <ComboBoxItem x:Name="Switch" Content="Switche"/> 
    <ComboBoxItem x:Name="Firewall" Content="Firewalle"/> 
    <ComboBoxItem x:Name="Host" Content="Hosty" /> 
    <ComboBoxItem x:Name="SRF1" Content="SRF1"/> 
</MyComboBox> 

實施的行爲:

一個行爲類允許您將行爲添加到使用XAML控制。行爲被編碼。

public class ComboBoxSelectionBehavior:Behavior<ComboBox> 
{ 
    protected override void OnAttached() 
    { 
    base.OnAttached(); 
    AssociatedObject.Loaded += ComboBoxLoaded; 
    } 

    protected override void OnDetaching() 
    { 
    AssociatedObject.Loaded -= ComboBoxLoaded; 
    base.OnDetaching(); 
    } 

    private void ComboBoxLoaded(object sender, RoutedEventArgs e) 
    { 
    if(Count > 1) 
    { 
     SelectedIndex = 1; 
    } 
    } 
} 

用法:

<ComboBox> 
    <Interactivity:Interaction.Behaviors> 
    <Behaviors:ComboBoxSelectionBehavior/> 
    </Interactivity:Interaction.Behaviors> 
    <ComboBoxItem x:Name="Switch" Content="Switche"/> 
    <ComboBoxItem x:Name="Firewall" Content="Firewalle"/> 
    <ComboBoxItem x:Name="Host" Content="Hosty" /> 
    <ComboBoxItem x:Name="SRF1" Content="SRF1"/> 
</ComboBox> 

注意,對於行爲的使用你所需要的混合SDK安裝。

0

我懷疑由於DataTemplate中的SelectionChanged事件處理程序而出現錯誤。我guessubg的DataGrid你使用這個模板中無法找到的事件處理程序。爲什麼你需要這個事件處理程序?

我出現以下XAML類似的錯誤消息:

MainPage.xaml中:

<UserControl x:Class="Example.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="400"> 
    <UserControl.Resources> 
     <ResourceDictionary Source="Dictionary1.xaml" /> 
    </UserControl.Resources> 
    <ItemsControl ItemsSource="ABC" ItemTemplate="{StaticResource failTemplate}" /> 
</UserControl> 

Dictionary1.xaml:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <DataTemplate x:Name="failTemplate"> 
     <ComboBox SelectionChanged="ComboBox_SelectionChanged"> 
      <ComboBoxItem Content="AAA" /> 
      <ComboBoxItem Content="BBB" /> 
      <ComboBoxItem Content="CCC" /> 
     </ComboBox> 
    </DataTemplate> 
</ResourceDictionary> 
+0

事件工作正常,沒有SelectedIndex屬性。 – neurotix