1

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
rohan_vg
  • 1,087
  • 3
  • 15
  • 27
  • Actully you are binded textblock in LongListSelector so you can not directly use it ... – Mansinh Oct 07 '13 at 09:43
  • I think (since you are using longlist selector) you should point to the index of the Longlistselection item you want to hide. Something like LonglistSelector.ItemIdex (something like that). But one thing is certain, you can't access it right now, because its a child of Longlist Selector. You can't access it directly – Bart Teunissen Oct 07 '13 at 09:43
  • first you have to find it than you can use it of textblock property – Mansinh Oct 07 '13 at 09:44
  • I did not know that. I will try your solution Bart. – rohan_vg Oct 07 '13 at 09:45
  • This should help: (http://msdn.microsoft.com/en-us/library/bb613579.aspx) – trinaldi Oct 07 '13 at 09:47

2 Answers2

3

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

Community
  • 1
  • 1
FunksMaName
  • 2,101
  • 1
  • 15
  • 17
  • Its giving me the following error. `The resource "VisibilityConverter" could not be resolved.` I'm sure this is a silly error on my part. – rohan_vg Oct 07 '13 at 11:21
  • Thanks. I got it working now. But something strange is happening. Its giving me an error `Invalid XAML.` But it runs with no issues. – rohan_vg Oct 07 '13 at 12:47
0

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

Community
  • 1
  • 1
Mansinh
  • 1,365
  • 4
  • 19
  • 47