Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. selected item for WPF ListBox [modified]

selected item for WPF ListBox [modified]

Scheduled Pinned Locked Moved WPF
wpfcsharpcssdatabasewcf
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    devvvy
    wrote on last edited by
    #1

    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

    B 1 Reply Last reply
    0
    • D devvvy

      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

      B Offline
      B Offline
      BechBej
      wrote on last edited by
      #2

      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

      D 1 Reply Last reply
      0
      • B BechBej

        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

        D Offline
        D Offline
        devvvy
        wrote on last edited by
        #3

        thanks but no, the list is built dynamically by binding to it a DataTable as shown in my code fragment

        dev

        B 1 Reply Last reply
        0
        • D devvvy

          thanks but no, the list is built dynamically by binding to it a DataTable as shown in my code fragment

          dev

          B Offline
          B Offline
          BechBej
          wrote on last edited by
          #4

          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

          D 1 Reply Last reply
          0
          • B BechBej

            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

            D Offline
            D Offline
            devvvy
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups