Error: name "tBox" does not exist in the current context

Error: the name 'tBox' does not exist in the current context.

XAML:

<ItemsControl Name="itemsControl">
    <ItemsControl.Template>
        <ControlTemplate>
           <WrapPenel>
               <ItemsPresenter/>
            </WrapPenel>
        </ControlTemplate>
    </ItemsControl.Template>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Name="tBox" Text="{Binding Name}"></TextBlock>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

WITH#:

tBox.Background=Brushes.White; // Error: The name 'tBox' does not exist in the current context.

How to access management?

+3
source share
5 answers

The TextBlock you called tBox is inside the DataTemplate. The controls inside the template are in a different namespace, so you cannot access it through code through your name. I'm not sure, but you can get it through the ItemTemplate property and pass it to a TextBlock. Or you can add a property to your code representing the background and use the binding to the TextBlock Background property. Hope this helps.

+1
source

TextBlock DataTemplate:

<DataTemplate>
    <TextBlock Name="tBox" Background="White" Text="{Binding Name}"></TextBlock>
</DataTemplate>

, Background , Triggers:

<DataTemplate>
    <TextBlock Name="tBox" Text="{Binding Name}"></TextBlock>
    <DataTemplate.Triggers>
        <Trigger SourceName="tBox" Property="IsMouseOver" Value="True">
            <Setter TargetName="tBox" Property="Background" Value="White" />
        </Trigger>
    </DataTemplate.Triggers>
</DataTemplate>

, , : WPF - 4 ( )

0

, , , :

DataTemplate

- :

var tbUserIcon= (TextBlock)checkBox.Template.FindName("tbUserIcon", checkBox);

, , , , intellisense .

0

this.Background = Brushes.White; ( , )?

-1
source

Since Background is a dependency property, you will need to use

tBox.SetValue (BackgroundProperty, new SolidBrush (Color.White));

-1
source

Source: https://habr.com/ru/post/1763749/


All Articles