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