tags:

views:

73

answers:

4

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?

A: 

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

JamesM
Wrong, this sets the background of the control, not the textbox
Arcturus
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
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
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
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
A: 

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

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

Mamta Dalal
Wrong.. You can set properties like he mentioned in the question
Arcturus
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 Dalal
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
A: 

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

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: http://www.codeproject.com/KB/WPF/GuidedTourWPF_4.aspx

Arcturus
I have a few template controls, and each joins his class. tBox1.ItemSource = Class1; tBox2.ItemSource = Class2; tBox2.ItemSource = Class3;
Keepq
I don't get it.. Can you post a bit more code?
Arcturus