none
How to get Visibilty TextBlock In ListBox

    Question

  • how can i get visibility textblock in listbox without get exception null reference?
    Monday, January 13, 2014 4:00 AM

All replies

  • Hi ,

    Your problem is not clear,so please elaborate the problem what actually you want to do?

    Thanks

    Monday, January 13, 2014 4:10 AM
  • code in xaml like this

    <Grid Margin="0,33,0,0">
                                <ListBox x:Name="feabox" SelectionChanged="feabox_SelectionChanged">
                                    <ListBox.ItemTemplate>
                                        <DataTemplate>
                                            <StackPanel Margin="0,0,0,30">
                                                <Grid>
                                                    <TextBlock FontSize="40"  Text="{Binding head}"/>
                                                </Grid>
                                                <Grid Height="80" Width="80" Margin="0,-10,-400,0">
                                                    <Image Source="{Binding logo}" Margin="0,-100,30,0"/>
                                                </Grid>
                                                <Grid Height="347" Margin="0,-50,0,0">
                                                    <Image Source="{Binding feim}"/>
                                                </Grid>
                                                <Grid x:Name="tes" Background="#AAAA" Margin="0,-350,0,0">
                                                    <TextBlock x:Name="yay"  FontSize="30" Text="{Binding fe}" Margin="0,5,0,0" HorizontalAlignment="Left" FontStretch="Expanded" Visibility="Collapsed"/>
                                                    <TextBox x:Name="yey" Text="a" Visibility="Collapsed"></TextBox>
                                                </Grid>
                                            </StackPanel>
                                        </DataTemplate>
                                    </ListBox.ItemTemplate>
                                </ListBox>
                            </Grid>

    for the cs

     private void feabox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                var elem = (FrameworkElement)sender;
                var tbx = (TextBox)elem.FindName("yey");
                tbx.Visibility = Visibility.Visible;
            }

    but when i select one of the listbox, i get null references exception, is there any solution?

    Monday, January 13, 2014 4:17 AM
  • An approach that follows MVVM pattern is to add a visibility property to your view model (which currently has head, logo, fe properties) and databind the Visibility.

        <TextBox x:Name="yey" Text="a" Visibility="{Binding viz}"></TextBox>

        private void feabox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            (feabox.SelectedItem as MyViewModelClass).viz = Visibility.Visible;
        }

    where MyViewModelClass is the type of item the ListBox is binding to, and viz is a property of type Visibility in the class.

    private Visibility _viz = Visibility.Collapsed;
    public Visibility viz
    {
        get
        {
            return _viz;
        }
        set
        {
            if (value != _viz)
            {
                _viz = value;
                NotifyPropertyChanged("viz");
            }
        }
    }


    Richard Woo
    Monday, January 13, 2014 6:35 AM
  • An approach that follows MVVM pattern is to add a visibility property to your view model (which currently has head, logo, fe properties) and databind the Visibility.

        <TextBox x:Name="yey" Text="a" Visibility="{Binding viz}"></TextBox>

        private void feabox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            (feabox.SelectedItem as MyViewModelClass).viz = Visibility.Visible;
        }

    where MyViewModelClass is the type of item the ListBox is binding to, and viz is a property of type Visibility in the class.

    private Visibility _viz = Visibility.Collapsed;
    public Visibility viz
    {
        get
        {
            return _viz;
        }
        set
        {
            if (value != _viz)
            {
                _viz = value;
                NotifyPropertyChanged("viz");
            }
        }
    }


    Richard Woo
    i've tried your code but it still same i get null references exception, do i need to add something that when i select one of my listbox not get exception null references?
    Monday, January 13, 2014 7:12 AM
  • Most likely you need to check if SelectedItem is null. This is a common check to do in the SelectionChanged handler. It means that no item is selected.

        private void feabox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (feabox.SelectedItem == null)
                return;
            (feabox.SelectedItem as MyViewModelClass).viz = Visibility.Visible;
        }

    If there is still a problem, I can show a full working example.


    Richard Woo
    Monday, January 13, 2014 8:58 AM
  •  Grid sPanel = (Grid)sender;
                var tbxSizeCollection = from tbx in sPanel.Children.OfType<TextBlock>()
                                        where tbx.Name == "size"
                                        select tbx;

    In your case your datatemplate is stack panel so you can use StackPanel in place of Grid.

    Regards,

    Pankaj

    Monday, January 13, 2014 10:04 AM