English 中文(简体)
布塔顿内装货箱的固定价值
原标题:Obtain value of textblock inside in a Button

I m writting aWindows Telephone App, in the GUI a have a list Box with many htons, some such as this

    <ListBox x:Name="List">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Button Width="460" Height="100" Click="Click_B">
                                    <Button.Content>
                                        <StackPanel Orientation="Horizontal" Height="80" Width="400">    
                                            <TextBlock Width="200" Name="txtblockName" FontSize="22" Text="{Binding Name}" Height="40"/>
                                            <TextBlock Width="200" Name="txtblockUrl" FontSize="22" Text="{Binding Url}" Height="40"/>
                                        </StackPanel>
                                    </Button.Content>
                                </Button>                            
                            </DataTemplate>
                        </ListBox.ItemTemplate>
</ListBox>

当我点击布顿时,我需要获得“txtblock Url”文本的内容,我如何能够获得这一价值?

private void Click_B(object sender, RoutedEventArgs e)
        {
            Button source = (Button)e.OriginalSource;

        }
最佳回答

你们可以打破下面所示的排位等级。

private void Click_B(object sender, RoutedEventArgs e)
{
  string s = ((((sender as Button).Content) as StackPanel).Children[1] as TextBlock).Text;
}

However, data binding a list of objects to your ListBox.ItemsSource is a better solution than this.

问题回答

了解情况的另一个途径是,你想要获得受你吨约束的物体的<代码>Name财产的价值。 您可在上找到这个物体。

If you replace MyType with the type of your bound object, something like this should do what you want:

private void Click_B(object sender, RoutedEventArgs e)
{
    Button source = (Button)e.OriginalSource;
    string name = ((MyType)source.DataContext).Name;
}

也许有一个更好的解决办法,但如果你希望直接提及,你就只能走下路。

private void Click_B(object sender, RoutedEventArgs e)
{
     Button source = (Button)e.OriginalSource;
     StackPanel stp = source.Content as StackPanel;
     TextBlock blk = stp.Children[1];
     //Whatever you needed could now reference blk.Text
}

www.un.org/Depts/DGACM/index_spanish.htm EDIT:我将采用上述数据具有约束力的解决办法。 这只是迅速和 d忙地查阅案文Block。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签