DaveDev
DaveDev

Reputation: 42185

How to access a value in a TextBlock?

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.

What do I need to do to access the value?

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

Upvotes: 0

Views: 4232

Answers (3)

ColinE
ColinE

Reputation: 70142

The backing variables generated by visual studio within the .xaml.cs file are only generated for certain circumstances. Any 'named' element within the body of a user control will have a generated backing variable. However, named elements within templates will not be generated. This is because Visual Studio has no way of knowing how your template will be used. For example, your template could be used by an ItemsControl to generate multiple template instances. What should be generated within .xaml.cs in that case?

You have two options:

  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.

For (2), I would suggest using Linq-to-VisualTree, where you can find your TextBlock as follows:

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

Upvotes: 1

daryal
daryal

Reputation: 14919

Maybe I did not get the point but why don't you create a binding to textbox and mark it as two way?

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

Upvotes: 0

Tigran
Tigran

Reputation: 62246

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

You need to read a Value

Try always to avoid access UI elements directly in WPF, cause sometimes (not so rare cases) it becomes really tricky to find them if not imossible (I mean not guranteed way). Access a Data that stands behind them.

Upvotes: 0

Related Questions