selected item for WPF ListBox [modified]
-
hello newbie to WPF, how do you set selected item for a list box?
SomeDataTab.Columns.Add("SomeData", typeof(string)); SomeDataTab.Columns.Add("IsSelected", typeof(bool)); Cmd = new SqlCommand(SQL, (SqlConnection)DevConn); ... Rdr = Cmd.ExecuteReader(); while (Rdr.Read()) { rw = SomeDataTab.NewRow(); SomeData = (string)Rdr["SomeData"]; if (SomeData == "TEMPLATE_OBJECTIVE") { SomeData = "SharedData"; rw["IsSelected"] = true; } else { rw["IsSelected"] = false; } rw["SomeData"] = SomeData; SomeDataTab.Rows.Add(rw); } SomlstSomeData.DataContext = SomeDataTab.DefaultView;
The above code shows how we bind to DataTable and following is xaml code: <ListBox Name="lstSomeData" Grid.Row="0" Grid.Column="0"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" /> <Label Content="{Binding SomeData}"></Label> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> I wish to know how to pre-select say an Item with value "AAA"? Thanks!dev
modified on Sunday, July 4, 2010 9:47 PM
-
hello newbie to WPF, how do you set selected item for a list box?
SomeDataTab.Columns.Add("SomeData", typeof(string)); SomeDataTab.Columns.Add("IsSelected", typeof(bool)); Cmd = new SqlCommand(SQL, (SqlConnection)DevConn); ... Rdr = Cmd.ExecuteReader(); while (Rdr.Read()) { rw = SomeDataTab.NewRow(); SomeData = (string)Rdr["SomeData"]; if (SomeData == "TEMPLATE_OBJECTIVE") { SomeData = "SharedData"; rw["IsSelected"] = true; } else { rw["IsSelected"] = false; } rw["SomeData"] = SomeData; SomeDataTab.Rows.Add(rw); } SomlstSomeData.DataContext = SomeDataTab.DefaultView;
The above code shows how we bind to DataTable and following is xaml code: <ListBox Name="lstSomeData" Grid.Row="0" Grid.Column="0"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}" /> <Label Content="{Binding SomeData}"></Label> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> I wish to know how to pre-select say an Item with value "AAA"? Thanks!dev
modified on Sunday, July 4, 2010 9:47 PM
-
Is that you require something like this [Code] [/Code] This code will show One item as selected by default at the compile time and not after firing the application
-
thanks but no, the list is built dynamically by binding to it a DataTable as shown in my code fragment
dev
You can do this progammatically by using the IsSelected = true of the given MenuItem in the code behid too if I well understand the question, I mean you can test the value AAA using the selected value property of the combo box and then set the IsSelected property of the corresponding item to true
-
You can do this progammatically by using the IsSelected = true of the given MenuItem in the code behid too if I well understand the question, I mean you can test the value AAA using the selected value property of the combo box and then set the IsSelected property of the corresponding item to true
Thanks no not really... In another post I've added more detail - basically (copied from there), I'm using CodePlex WPF ComboBox and is having trouble pre-select item From code behind, I bind the combo box to a DataTable with two columns: ... SomeTableSrc = new DataTable("SomeData"); SomeTableSrc.Columns.Add("Data", typeof(string)); SomeTableSrc.Columns.Add("IsSelected", typeof(bool)); lstSomeData.DataContext = SomeTableSrc.DefaultView; Xaml: <Style TargetType="{x:Type ComboBoxItem}"> </Style> The above will select relevant "ComboBoxItem" (and it works fine, if you click on ComboBox, as menu expands you can see desired items actually get selected), *** BUT *** "SelectedValue" or "Text" of "ComboBox" (not items) remains blank - and setting these "ComboBox" attributes directly just don't work. // Don't work, "ComboBox" text remains blank lstSomeData.SelectedValue = "ABC"; // Don't work, "ComboBox" text remains blank lstSomeData.Text = "ABC";
dev
modified on Wednesday, July 7, 2010 10:09 PM