rohan_vg
rohan_vg

Reputation: 1129

WP programmatically reference TextBlock in C#

I am trying to is disable the visibility of the TextBlock. I can reference the TextBlock in the following example:

XAML File

<phone:PivotItem Header="Pivot 1">
   <TextBlock Text="hello world" x:Name="dummytext" />
</phone:PivotItem>

CS File

dummytext.Visibility = Visibility.Collapsed;

But I can't reference it when I have this following code:

XAML File

<phone:PivotItem Header="{Binding Dummy.Title}">
  <Grid Margin="0,0,-12,0">
    <ListBox x:Name="Box1">
      <phone:LongListSelector ItemsSource="{Binding Dummy.Items}">
        <phone:LongListSelector.ItemTemplate>
          <DataTemplate>
            <StackPanel>

              <Grid>
                //REFERENCE THIS TEXTBLOCK
                <TextBlock Text="hello world" x:Name="dummytext" />
              </Grid>

              <Grid>
                <TextBlock Text="byee world" x:Name="dummytext2" />
                <TextBlock Text="bye2 world" x:Name="dummytext3" />
              </Grid>

            </StackPanel>
          </DataTemplate>
        </phone:LongListSelector.ItemTemplate>
      </phone:LongListSelector>
    </ListBox>
  </Grid>
</phone:PivotItem>

I am new to Windows Phone development and still learning. Can you point me to where I am going wrong?

Upvotes: 1

Views: 744

Answers (2)

FunksMaName
FunksMaName

Reputation: 2111

If you are trying to set the visibility of a control, a suitable approach would be for you to use a visibility “converter”, send a property in your entity to the converter and then return the desired Visibility state.

 public class VisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool visible;

        bool.TryParse(value.ToString(), out visible);

        return visible ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Define your converter

You can place this in the app.xaml file so that you whole app has access to it when required.

<Application  xmlns:converters="clr-namespace:NamespaceOfYourConverter;assembly=AssemplyOfYourConverter">
   <Application.Resources>
    <ResourceDictionary>
        <converters:VisibilityConverter x:Key="VisibilityConverter" />
    </ResourceDictionary>
   </Application.Resources>
</Application>

Set your xaml

<TextBlock Text="hello world" x:Name="dummytext" Visibility="{Binding IsVisible, Converter={StaticResource VisibilityConverter}}" />

see Converters or Bing "value converters wp8" for more on creating converters

Upvotes: 3

Mansinh
Mansinh

Reputation: 1435

FInd your textblock from LongListSelector through visual tree than use propert of textblock

refere below link

How to find a specific element inside a control using Visual tree in WP7

Visual Tree Enumeration

Find image control using visual tree

Upvotes: 0

Related Questions