0
votes

My listbox XAML looks like this:

<ListBox x:Name="lstbxbProducts" SelectionChanged="lstbxbProducts_SelectionChanged_1" HorizontalAlignment="Left" Height="547" Margin="0,221,0,0" VerticalAlignment="Top" Width="1044" RenderTransformOrigin="0.600000023841858,0.5">        
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <StackPanel Width="80" VerticalAlignment="Center" Orientation="Horizontal">
                    <TextBlock Text="{Binding prdnum}" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
                    <TextBlock Text="  -" VerticalAlignment="Center" HorizontalAlignment="Left" ></TextBlock>
                </StackPanel>
                <StackPanel Width="400">
                    <TextBlock Text="{Binding prddsc}" VerticalAlignment="Center"  HorizontalAlignment="Left" ></TextBlock>
                </StackPanel>
                <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Width="180">
                    <StackPanel>
                        <TextBlock Text="{Binding um_cod}" Name="txtblkUmcode" VerticalAlignment="Center"  HorizontalAlignment="Left" ></TextBlock>
                    </StackPanel>
                    <StackPanel Width="20"></StackPanel>
                    <StackPanel Width="20">
                        <Image Source="/Images/Arrowselection.png" Tapped="Image_Tapped_1" VerticalAlignment="Center" ></Image>
                    </StackPanel>
                </StackPanel>

                <StackPanel Width="180">
                    <TextBlock Text="{Binding prcby_prc}" VerticalAlignment="Center" HorizontalAlignment="Center" ></TextBlock>
                </StackPanel>
                <StackPanel Width="100">
                    <TextBox Text="{Binding stdordqty, Mode=TwoWay}" VerticalAlignment="Center" TextAlignment="Center"   HorizontalAlignment="Right" ></TextBox>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

I want to set the text of Name="txtblkUmcode" present in my listbox from c# codebhind in this way, but no luck:

TextBlock txtBlk = new TextBlock();
txtBlk =  lstbxbProducts.FindName("txtblkUmcode") as TextBlock;
txtBlk .Text = "test";

Please let me know how can I:

  1. set the text of that textblock prsent inside a datatemplate of a my listbox ?
  2. i want to change the text of that textblock of that single listbox item only ?

How can I do this? Please let me know.

1

1 Answers

0
votes

You have to avoid this kind of programming practice because if the view changes - for example there comes a modified DataTemplate with other controls displaying that property(Label, TextBox, etc) - can break your application.

Sometimes there is no other solution.

See the answer here: http://msdn.microsoft.com/en-us/library/bb613579.aspx