3

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>

C#:

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

How to access control?

D.R.
  • 20,268
  • 21
  • 102
  • 205
Keepq
  • 123
  • 3
  • 9

5 Answers5

1

The TextBlock you named tBox is inside a DataTemplate. Controls inside a template are in a different name scope, so you can't access it in code-behind via its name. I'm not sure but you might get it via the ItemTemplate property and casting it to a TextBlock. Or you can add a property in your code-behind representing the background and use binding on the TextBlock's Background property. Hope this helps.

Michael Detras
  • 211
  • 2
  • 5
0

Set it on the TextBlock, in your DataTemplate:

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

Or if you wish to only set the Background in certain conditions, consider using 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>

More information on how to use Triggers can be found here: A Guided Tour of WPF - Part 4 (Data Templates and Triggers)

jszigeti
  • 373
  • 4
  • 11
Arcturus
  • 26,677
  • 10
  • 92
  • 107
  • I have a few template controls, and each joins his class. tBox1.ItemSource = Class1; tBox2.ItemSource = Class2; tBox2.ItemSource = Class3; – Keepq Sep 08 '10 at 12:24
0

I didn't try but maybe the answer here works:

Access a control from within a DataTemplate with its identifying name

to use something like :

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

But I think this way isn't convenient at all, especially if there's lots of controls have to do it this way, and it can't be checked by intellisense when writing code real time.

Community
  • 1
  • 1
yu yang Jian
  • 6,680
  • 7
  • 55
  • 80
-1

this.Background=Brushes.White; (assuming its code behind the control)?

JamesM
  • 1,048
  • 1
  • 10
  • 24
  • Wrong, this sets the background of the control, not the textbox – Arcturus Sep 08 '10 at 09:33
  • Placing this in your code behind will put the Background to White of your control. Not of the tBox, since it is not known in the code behind, only in the DataTemplate. I suggest you try the code sample he posted, change your color to Red, and see what happens. – Arcturus Sep 08 '10 at 13:01
  • Arcturus, you are correct but please try being a bit more polite with the structure of response. Puts people off trying to reply, like me! :) – JamesM Sep 08 '10 at 13:04
  • Sure James, but you might try to post a correct answer to start with ;). Don't take min votes personal; try to learn from them and become a better programmer in the end. – Arcturus Sep 08 '10 at 13:09
  • Yep, totally agree. Its just all in the wording. I dont mind min votes if they are correct (which yours was, as I should have ready it more clearly instead of answering quickly). – JamesM Sep 08 '10 at 13:11
-1

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

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

Mamta D
  • 6,310
  • 3
  • 27
  • 41
  • Wrong.. You can set properties like he mentioned in the question – Arcturus Sep 08 '10 at 09:33
  • No, you CANNOT set the properties via code in the.cs class. I tested his code again and confirmed this. It can only be set in XAML like you suggested but not through code. – Mamta D Sep 09 '10 at 03:50
  • Yes correct. Its because tBox resides in a different namescope -> its in the DataTemplate. When your ItemsSource has 3 items, you will have 3 tBoxes – Arcturus Sep 09 '10 at 07:48