question

naivedev101-2726 avatar image
0 Votes"
naivedev101-2726 asked naivedev101-2726 answered

change textbox text on combobox value selection(Comboitem is used not itemsource). WPF

Hello!

I have been stucked on this issue of changing the textbox text on selection of comboitem.

So, the scene is I have a xml file where name and value tags are present name tags are having same values as combobox.

i have to check if combobox selected item is in xml then show the value assigned in xml file for the selected item in the text box.

 #this is my xaml for combobox and textbox
    
 <DataTemplate x:Name="dtSettingValue" x:Key="dtSettingValue">
                         <DockPanel>
                             <TextBlock Text="{Binding Path=Field, Converter={StaticResource FieldConverter}}" VerticalAlignment="Center">
                                 <TextBlock.Style>
                                     <Style TargetType="TextBlock">
                                         <Setter Property="OverridesDefaultStyle" Value="False"/>
                                         <Style.Triggers>
                                             <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" Value="False">
                                                 <Setter Property="Visibility" Value="Visible"/>
                                             </DataTrigger>
                                             <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" Value="True">
                                                 <Setter Property="Visibility" Value="Collapsed"/>
                                             </DataTrigger>
                                         </Style.Triggers>
                                     </Style>
                                 </TextBlock.Style>
                             </TextBlock>
                             <ComboBox SelectedIndex="{Binding Path=Field}" SelectionChanged="Field_SelectionChanged" x:Name="field_Combo">
                                 <ComboBoxItem Content="{Binding ConverterParameter=CITY1, Converter={StaticResource StringsResourceConverter}, Mode=OneTime}"/>
                                 <ComboBoxItem Content="{Binding ConverterParameter=CITY2, Converter={StaticResource StringsResourceConverter}, Mode=OneTime}"/>
                                 <ComboBoxItem Content="{Binding ConverterParameter=CITY3, Converter={StaticResource StringsResourceConverter}, Mode=OneTime}"/>
                                 <ComboBoxItem Content="{Binding ConverterParameter=CITY4, Converter={StaticResource StringsResourceConverter}, Mode=OneTime}"/>
                                    
                                 <ComboBox.Style>
                                     <Style TargetType="ComboBox">
                                         <Setter Property="SnapsToDevicePixels" Value="true"/>
                                         <Setter Property="OverridesDefaultStyle" Value="true"/>
                                         <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
                                         <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
                                         <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
                                         <Setter Property="Foreground" Value="{StaticResource Orange}"/>
                                         <Setter Property="MinWidth" Value="80"/>
                                         <Setter Property="Template">
                                             <Setter.Value>
                                                 <ControlTemplate TargetType="ComboBox">
                                                     <Grid>
                                                         <ToggleButton Name="ToggleButton" Template="{StaticResource ComboBoxToggleButton}" Grid.Column="2" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"/>
                                                         <ContentPresenter
                                                           Name="ContentSite"
                                                           IsHitTestVisible="False" 
                                                           Content="{TemplateBinding SelectionBoxItem}"
                                                           ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                                           ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                                           Margin="3,3,23,3"
                                                           VerticalAlignment="Center"
                                                           HorizontalAlignment="Left" />
                                                         <TextBox x:Name="PART_EditableTextBox"
                                                           Style="{x:Null}" 
                                                           Template="{StaticResource ComboBoxTextBox}" 
                                                           HorizontalAlignment="Left" 
                                                           VerticalAlignment="Center" 
                                                           Margin="3,3,23,3"
                                                           Focusable="True" 
                                                           Background="Transparent"
                                                           Foreground="{StaticResource Orange}"
                                                           CaretBrush="{StaticResource Orange}"
                                                           IsReadOnly="{TemplateBinding IsReadOnly}"/>
                                                         <Popup 
                                                           Name="Popup"
                                                           Placement="Bottom"
                                                           IsOpen="{TemplateBinding IsDropDownOpen}"
                                                           AllowsTransparency="True" 
                                                           Focusable="False"
                                                           PopupAnimation="Slide">
                                                             <Grid 
                                                                 Name="DropDown"
                                                                 SnapsToDevicePixels="True"                
                                                                 MinWidth="{TemplateBinding ActualWidth}"
                                                                 MaxHeight="{TemplateBinding MaxDropDownHeight}">
                                                                 <Border x:Name="DropDownBorder" Background="#888888" BorderThickness="2" BorderBrush="{StaticResource TextBoxForegroundBrush}"/>
                                                                 <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                                                                     <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                                                                 </ScrollViewer>
                                                             </Grid>
                                                         </Popup>
                                                     </Grid>
                                                     <ControlTemplate.Triggers>
                                                         <Trigger Property="HasItems" Value="false">
                                                             <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
                                                         </Trigger>
                                                         <Trigger Property="IsGrouping" Value="true">
                                                             <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                                                         </Trigger>
                                                         <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
                                                             <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
                                                             <Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
                                                         </Trigger>
                                                         <Trigger Property="IsEditable" Value="true">
                                                             <Setter Property="IsTabStop" Value="false"/>
                                                             <Setter TargetName="PART_EditableTextBox" Property="Visibility"    Value="Visible"/>
                                                             <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
                                                         </Trigger>
                                                     </ControlTemplate.Triggers>
                                                 </ControlTemplate>
                                             </Setter.Value>
                                         </Setter>
                                         <Style.Triggers>
                                             <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" Value="False">
                                                 <Setter Property="Visibility" Value="Collapsed"/>
                                             </DataTrigger>
                                             <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" Value="True">
                                                 <Setter Property="Visibility" Value="Visible"/>
                                             </DataTrigger>
                                         </Style.Triggers>
                                     </Style>
                                 </ComboBox.Style>
                             </ComboBox>
                         </DockPanel>
                     </DataTemplate>
                        
                     <DataTemplate x:Name="dtSettingName" x:Key="dtSettingName">
                         <DockPanel>
                             <TextBlock Text="{Binding Path=TagName}">
                                 <TextBlock.Style>
                                     <Style TargetType="TextBlock">
                                         <Setter Property="OverridesDefaultStyle" Value="False"/>
                                         <Style.Triggers>
                                             <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" Value="False">
                                                 <Setter Property="Visibility" Value="Visible"/>
                                             </DataTrigger>
                                             <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" Value="True">
                                                 <Setter Property="Visibility" Value="Collapsed"/>
                                             </DataTrigger>
                                         </Style.Triggers>
                                     </Style>
                                 </TextBlock.Style>
                             </TextBlock>
                             <TextBox Text="{Binding Path=TagName}">
                                 <TextBox.Style>
                                     <Style TargetType="TextBox">
                                         <Setter Property="SnapsToDevicePixels" Value="True"/>
                                         <Setter Property="OverridesDefaultStyle" Value="True"/>
                                         <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
                                         <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                                         <Setter Property="Foreground" Value="{DynamicResource TextBoxForegroundBrush}"/>
                                         <Setter Property="CaretBrush" Value="{DynamicResource TextBoxForegroundBrush}"/>
                                         <Setter Property="Background" Value="#000000"/>
                                         <Setter Property="AllowDrop" Value="true"/>
                                         <Setter Property="Template">
                                             <Setter.Value>
                                                 <ControlTemplate TargetType="{x:Type TextBoxBase}">
                                                     <Border Name="Border" CornerRadius="2" Padding="2" Background="#333333" BorderBrush="#888888" BorderThickness="1">
                                                         <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
                                                     </Border>
                                                 </ControlTemplate>
                                             </Setter.Value>
                                         </Setter>
                                         <Style.Triggers>
                                             <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" Value="False">
                                                 <Setter Property="Visibility" Value="Collapsed"/>
                                             </DataTrigger>
                                             <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}" Value="True">
                                                 <Setter Property="Visibility" Value="Visible"/>
                                             </DataTrigger>
                                         </Style.Triggers>
                                     </Style>
                                 </TextBox.Style>
                             </TextBox>
                         </DockPanel>
                     </DataTemplate>

i added the selection change on combobox to set the textbox values but i am not able to control the textbox name(if i set the x:Name="" in text box )

below is the selection change function on combobox selection change.

 private void Field_SelectionChanged(object sender, SelectionChangedEventArgs e)
         {
                
             var selectedField = lstSettings.SelectedItems.Cast<FieldCustomTag>().ToArray(); //This used to get the whole row including combobox and textbox
             if (lstSettings.Items.Count > 1)
             {
               
                 string currDir = Environment.CurrentDirectory;
                 if (Directory.Exists(currDir))
                 {
    
                        
                     XmlDocument doc = new XmlDocument();
                     doc.Load(currDir + "\\cityDetails.xml");
                     for (int i = 0; i < doc.DocumentElement.ChildNodes.Count; i++)
                     {
                         if (selectedField.First().Field == i)
                         {
                             selectedField.First().TagName.Replace(selectedField.First().TagName.ToString(), doc.DocumentElement.ChildNodes[i].LastChild.InnerText.ToString());
                             break;
                         }
                         else
                             continue;
                            
                     }
                        
                 }
             }
    
                
    
         }

this is what my xml file contains.


<Setting>
<Name>London</Name>
<Value>Lords</Value>
</Setting>
<Setting>
<Name>Kolkata</Name>
<Value>Eden Garden</Value>
</Setting>
<Setting>
<Name>Melbourne</Name>
<Value>MCG</Value>
</Setting>

so the combobox values i've added in comboitem and they are getting their value from stringresource.

on textbox i have to add value from xml with respective of selection being made.

Thanks in advance!

windows-wpfdotnet-wpf-xaml
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,naivedev101-2726.What is Field bound to your ComboBox ? Is it the data in xml? Or collection? If the ComboBox is not directly bound to the data in the xml file, please show the data source and its relationship with the data in the xml file. Is it possible to show the code of FieldConverterStrings and ResourceConverter?

i have to check if combobox selected item is in xml then show the value assigned in xml file for the selected item in the text box.
so the combobox values i've added in comboitem and they are getting their value from stringresource. 
on textbox i have to add value from xml with respective of selection being made.

What is your stringresource?
Do you want to display its value in the TextBox based on the item selected by Commbox?

137290-2.gif

0 Votes 0 ·
2.gif (38.6 KiB)

Hello HuiLiu-MSFT!

Sorry for the late reply. I solved this issue by implementing NotifyPropertyChanged.

The fields Im using for showing ComboBox text and its value are basically property of a class so I've to implement property change event to reflect the changes in real time.


Yes, I wanted to show values of a selected item from a ComboBox into a adjacent TextBox.

ResourceConverter is for app language translation(Globalization). Like English, French German.


Thank you for responding to my question.

Much appreciated!! :)

·

0 Votes 0 ·

1 Answer

naivedev101-2726 avatar image
0 Votes"
naivedev101-2726 answered

ANSWER:

NotifyPropertyChange event implemented to show the UI change in real time.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.