設置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)
任何想法?
可能的問題是添加項目之前選定的指標設置?您可以嘗試在加載窗口或控件後設置索引。除此之外:SelectionChanged方法正在做什麼? – MatthiasG
選擇變更可以簡單地重定向到另一個網頁。沒有花哨,沒有SelectedIndex屬性工作正常。嗯,我怎麼可以從後面的頁面代碼訪問DataTemplate對象屬性? 看起來像這樣.DeviceComboBox不起作用。 – neurotix
如果你想創建一個具有特殊行爲的組合框,我會建議創建一個從ComboBox派生並實現你的行爲的新類。這應該做的工作,你不需要數據模板。另一種方法是創建一個行爲元素。 – MatthiasG