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. WPF- Databinding set path

WPF- Databinding set path

Scheduled Pinned Locked Moved WPF
wpfcsharpwcfhelp
3 Posts 2 Posters 1 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
    DeepakMega
    wrote on last edited by
    #1

    HI, This is a simple one, but i cant get it to work I have a combobox filled with color names and a small colored rectangle next to the names. I want to bind this combobox to a text box, such that when a color is selected in combobox, only the color name shows up in the text box. I need to do this programatically, not in xaml My combobox datatemplate is <DataTemplate x:Key="ColorText" > <StackPanel Orientation="Horizontal"> <Rectangle Width="20" Height="28" Fill="{Binding Name}" Stroke="#FF000000"/> <TextBlock Text ="{Binding Name}" /> </StackPanel> </DataTemplate> My current code to bind is Binding myBinding = new Binding("Text"); myBinding.Source = this.colorTextBox; myBinding.Mode = BindingMode.Default; this.comboBox.SetBinding(ComboBox.SelectedItemProperty, myBinding); But the result i get in text box is "System.Windows.Media.Color Blue" , i need only Blue to appear in the text box. comboBox.selectedItem object contains 2 types, one is color and other is Name as the datatemplate shows. Please help me to modify my above binding code , such that the text box shows only the name of the color and not "System.Windows.Media.Color Blue". These controls(combobox and textbox) are programatically generated.

    I 1 Reply Last reply
    0
    • D DeepakMega

      HI, This is a simple one, but i cant get it to work I have a combobox filled with color names and a small colored rectangle next to the names. I want to bind this combobox to a text box, such that when a color is selected in combobox, only the color name shows up in the text box. I need to do this programatically, not in xaml My combobox datatemplate is <DataTemplate x:Key="ColorText" > <StackPanel Orientation="Horizontal"> <Rectangle Width="20" Height="28" Fill="{Binding Name}" Stroke="#FF000000"/> <TextBlock Text ="{Binding Name}" /> </StackPanel> </DataTemplate> My current code to bind is Binding myBinding = new Binding("Text"); myBinding.Source = this.colorTextBox; myBinding.Mode = BindingMode.Default; this.comboBox.SetBinding(ComboBox.SelectedItemProperty, myBinding); But the result i get in text box is "System.Windows.Media.Color Blue" , i need only Blue to appear in the text box. comboBox.selectedItem object contains 2 types, one is color and other is Name as the datatemplate shows. Please help me to modify my above binding code , such that the text box shows only the name of the color and not "System.Windows.Media.Color Blue". These controls(combobox and textbox) are programatically generated.

      I Offline
      I Offline
      Ian Shlasko
      wrote on last edited by
      #2

      Use a converter. Create a class that implements IValueConverter, and in the Convert() function, change it from "System.Windows.Media.Color.Blue" to "Blue" with some simple string parsing. Then just set it as the Converter property of the binding. Your code is a bit confusing, though... You're binding the selected item to a textbox? Does the Name property contain an actual color object, or a name of a color? Something doesn't seem quite right.

      Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

      D 1 Reply Last reply
      0
      • I Ian Shlasko

        Use a converter. Create a class that implements IValueConverter, and in the Convert() function, change it from "System.Windows.Media.Color.Blue" to "Blue" with some simple string parsing. Then just set it as the Converter property of the binding. Your code is a bit confusing, though... You're binding the selected item to a textbox? Does the Name property contain an actual color object, or a name of a color? Something doesn't seem quite right.

        Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

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

        The name property contains only a string eg. "Blue" The comboBox is filled with colors from Colors list. Helper h=new Helper(); this.comboBox.ItemsSource =h.GetPropNames(typeof(Colors)); The first property is a color and second one is Name, hence the full string looks like this "System.Windows.Media.Color.Blue Blue". I want to bind the comboBox.selectedItem.Name to the textbox, so the textbox shows only "Blue" I used value converters and it works , Thank you

        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