我是wpf的新手并且已经和这个问题斗争了好几天了。我有一个组合框,其dataview作为itemssource。它正确显示值,但重新运行应用程序时,selecteditem始终为null。
组合框绑定的ChargeCodeValidValues对象:
public class ChargeCodeValidValues
{
#region Properties
public DataTable ChargeCodesValidValuesTable { get; set; }
public DataView dvChargeCodeValidValues { get; set; }
#endregion
#region Constructor
public ChargeCodeValidValues()
{
LoadChargeCodesValidValues();
}
public void LoadChargeCodesValidValues()
{
Database db = new Database();
DataTable dataTable = db.ExecuteQuery("upGet_ChargeCodesValidValues", "ChargeCodesValidValues", "ID");
this.ChargeCodesValidValuesTable = dataTable;
this.dvChargeCodeValidValues = ChargeCodesValidValuesTable.DefaultView;
}
XAML:
<DataTemplate x:Key="combodescriptionTemplate">
<ComboBox Name="cboCombo"
Loaded="cboCombo_Loaded">
<ComboBox.DataContext>
<Objects:ChargeCodeValidValues/>
</ComboBox.DataContext>
</ComboBox>
</DataTemplate>
<local:TotalCellTemplateSelector x:Key="totalcellTemplateSelector"
combodescriptionTemplate="{StaticResource combodescriptionTemplate}"/>
gridview专栏:
<dxg:GridColumn Header="Description" FieldName="Description" Width="Auto" MinWidth="132" AllowMoving="False" CellTemplateSelector="{StaticResource totalcellTemplateSelector}" /> -->
此列默认为文本框。它会根据其他列的值
更改为组合框代码:
private void cboCombo_Loaded(object sender, RoutedEventArgs e)
{
ComboBox cmb = sender as ComboBox;
DataTable dtChargeCodeValidValues = oChargeCodesValidvalues.ChargeCodesValidValuesTable.Copy();
DataView dvCurrentCodeValues = dtChargeCodeValidValues.Copy().DefaultView;
cmb.ItemsSource = dvCurrentCodeValues;
cmb.DisplayMemberPath = "Description";
cmb.SelectedValuePath = "Description";
cmb.SelectedValue = "Description";
}
答案 0 :(得分:0)
在应用程序的不同调用中,ComboBox
中的选择不会持久存在。你必须自己编写代码。例如:
<ComboBox
x:Name="comboBox"
SelectionChanged="OnSelectionChanged"
Loaded="OnComboBoxLoaded"
>
<ComboBox.Items>
<system:String>One</system:String>
<system:String>Two</system:String>
<system:String>Three</system:String>
</ComboBox.Items>
</ComboBox>
...使用以下代码隐藏:
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
int index = comboBox.SelectedIndex;
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = store.CreateFile("selectedIndex"))
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(index.ToString());
}
}
}
}
private void OnComboBoxLoaded(object sender, RoutedEventArgs e)
{
int index = GetPreviousIndex();
comboBox.SelectedIndex = index;
comboBox.Text = comboBox.Items[index] as string;
}
private int GetPreviousIndex()
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = store.OpenFile("selectedIndex", FileMode.Open))
{
using (BinaryReader reader = new BinaryReader(stream))
{
try
{
return int.Parse(reader.ReadString());
}
catch (Exception x)
{
return -1;
}
}
}
}
}
请注意,设置SelectedIndex
是不够的;您还必须设置Text
属性,否则您无法在不打开ComboBox
的情况下看到所选内容。
当然,有很多方法可以坚持选择;在这里,我只是快速拼凑了一些东西(有些肮脏)。