0

我的列表框 XAML 如下所示:

<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>

我想以这种方式从 c# codebhind 设置我的列表框中的 Name="txtblkUmcode" 的文本,但没有运气:

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

请让我知道我该怎么做:

  1. 在我的列表框的数据模板中设置该文本块的文本?
  2. 我只想更改该单个列表框项目的文本块的文本?

我怎样才能做到这一点?请告诉我。

4

1 回答 1

0

您必须避免这种编程实践,因为如果视图发生更改——例如,修改后DataTemplate的其他控件显示该属性(Label、、TextBox等)——可能会破坏您的应用程序。

有时没有其他解决方案。

在此处查看答案:http: //msdn.microsoft.com/en-us/library/bb613579.aspx

于 2012-09-12T09:39:30.017 回答