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

naive_dev101 96 Reputation points
2021-10-01T09:12:58.33+00:00

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!

Developer technologies Windows Presentation Foundation
Developer technologies XAML
{count} votes

Accepted answer
  1. naive_dev101 96 Reputation points
    2021-10-14T04:54:54.557+00:00

    ANSWER:

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

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.