如何设置c#codebehind中列表框的datatemplate中存在的textblock文本?

时间:2012-09-12 09:08:46

标签: wpf silverlight xaml microsoft-metro

我的列表框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. 在我的列表框的数据窗口中设置该文本块prsent的文本吗?
  2. 我想仅更改该单个列表框项目的文本块的文本?
  3. 我该怎么做?请告诉我。

1 个答案:

答案 0 :(得分:0)

你必须避免这种编程习惯,因为如果视图发生了变化 - 例如,出现了修改后的DataTemplate,其他控件显示该属性(LabelTextBox等) - 可能会破坏您的申请。

有时没有其他解决方案。

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