English 中文(简体)
如何在文本Block上获取价值?
原标题:How to access a value in a TextBlock?
  • 时间:2012-04-11 10:38:02
  •  标签:
  • c#
  • wpf
  • xaml

I have a window with the following elements, and I m trying to access the value contained in <TextBlock Name="armingValue" but in my .xaml.cs file it doesn t seem to be recognised.

我需要做些什么来获得价值?

<Window.Resources>
    <DataTemplate DataType="{x:Type ArmingVM:ArmingItem}">
        <CheckBox Margin="10,5" IsChecked="{Binding IsSet}" Content="{Binding Name}"/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type ArmingVM:ArmingBindingData}">
        <DockPanel>
            <ItemsControl ItemsSource="{Binding ArmingItems}" HorizontalAlignment="Left">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Vertical"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
            <TextBlock Text="Enum Value: " HorizontalAlignment="Right"/>
            <TextBlock Name="armingValue" Text="{Binding Value}" HorizontalAlignment="Right"/>
        </DockPanel>
    </DataTemplate>
</Window.Resources>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="193*" />
        <ColumnDefinition Width="551*" />
    </Grid.ColumnDefinitions>

    <Button Content="Get Panel Options" Name="btnGetOptionsConfigruation" Margin="12,12,23,396" Click="btnGetOptionsConfigruation_Click"></Button>

    <StackPanel Grid.Column="1" Height="325" HorizontalAlignment="Left" Margin="68,43,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="438">
        <ItemsControl Name="armingItemsControl" ItemsSource="{Binding}"/>
    </StackPanel>
</Grid>
最佳回答

You do not need to access TextBox value but its binded value. So considering that you have in XAML

 <TextBlock Name="armingValue" Text="{Binding Value}" HorizontalAlignment="Right"/>

页: 1 价值

总是要避免直接进入<代码>WPF中的验证要素,有时会造成(并非如此少见的)直截了<>>。 查阅Data,背后。

问题回答

视力室在<代码>.xaml.cs文档中产生的辅助变量仅针对某些情况生成。 用户控制机构内的任何指定要素都将产生背后变量。 然而,在模板中不会产生指定的内容。 这是因为视觉演播室无法知道如何使用你的模板。 例如,可通过<代码>ItemsControl来生成多个模板。 在这种情况下,应在<代码>.xaml.cs内产生什么?

你有两种选择:

  1. Use binding, so that the state of your TextBlock.Text property is bound to a view model, so that you do not have to access the TextBlock element directly.
  2. walk the visual tree to locate your TextBlock at runtime.

关于(2),我建议使用Linq-to-VisualTree,请查阅:

TextBlock block = layoutRoot.Descendants<TextBlock>()
                            .Cast<TextBlock>().Where(tb => tb.Name="armingValue")
                            .Single();

难道我不问,但为什么不给你造成对文本箱的约束力,而是用两种方式加以标识?

<TextBlock Text="Enum Value: " HorizontalAlignment="Right" Text="{Binding Value, Mode=TwoWay}"/>




相关问题
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. ...

热门标签