2

For most of you, this might be an easy question, but I am a C# beginner (coming from VB) and would like to progam a Windows Phone App.

The question is: How can I access the TextBlock "LineOne" from code to change its width? For the page title, it works perfect with this (on orientation change):

this.PageTitle.Text = "Portrait";

However, something like this:

this.LineOne.width= "50";

won't work. Why?

My XAML looks like this (almost the default data bound app from Visual Studio Express):

    <!--TitlePanel -->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock x:Name="PageTitle" Text="Bundesliga" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel -->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}" SelectionChanged="MainListBox_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel x:Name="ListboxPanel" Margin="0,0,0,17" Width="432" Orientation="Horizontal">
                        <TextBlock x:Name="LineOne" Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource  PhoneTextNormalStyle}" Width="40" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

Thanks for your help!

1
  • 1
    Your ListBox can have multiple entries, therefore there's no one TextBlock called 'LineOne'. I can think of a hack that would let you change the width of all the textblocks in the listbox - bind the width in the datatemplate to the width of some control on your form. But I'll leave it to the WPF experts to give the right answer. Commented Aug 26, 2011 at 13:14

3 Answers 3

2

You have to access the TextBlocks inside the listbox. Something like:

TextBlock textblock = ListboxPanel.Items[index] as TextBlock;
textblock.Width = 50
Sign up to request clarification or add additional context in comments.

Comments

0
IEnumerable<TextBlock> listtb = ListboxPanel.Items.TypeOf<TextBlock>(); 

Comments

0

The name can't be resolved as belonging to this as it belongs to the datatemplate. You can't refer to an item within a template (from outside that template) as there could be multiple items with that name and names must be unique.

If you are trying to change the style of the selected item you will likely find a better solution to be to use different visual states to represent this.

If you are trying to access a property which relates to the bound viewmodel you can cast the sender to the type of the viewmodel and access it's properties directly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.