0

I am Working on Windows phone 8.1.I Making Simple ListView Demo Which is Contain two Image And Two TextBlock.

     <ListView x:Name="lst1" Grid.ColumnSpan="2"  >

        <ListView.ItemTemplate>
        <DataTemplate >
                <Grid>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <Image x:Name="imgSender" Source="Assets/button_register.png"  Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="8,15,8,8" />
                        <TextBlock  Text="{Binding Sender}" FontSize="18" Foreground="Black" FontWeight="Bold" HorizontalAlignment="Center" Margin="0,12,0,0"/>

                    <Image x:Name="imgReceiver" Source="Assets/button_register.png"  Grid.Row="1" Stretch="None" VerticalAlignment="Top" Margin="4,0" />
                    <TextBlock Text="{Binding Receiver}"  FontSize="18" Foreground="Black" FontWeight="Bold" HorizontalAlignment="Center" Grid.Row="1"  VerticalAlignment="Top"/>
                    <Image Source="Assets/scroll_line_addcategory.png" Grid.Row="2" Stretch="None" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="4,8,4,0" />
                </Grid>


            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

And My Problem is That I Want to Set the Visibility of 'imgSender' at Runtime in Xaml.cs File. Is Anybuddy Have Any Idea For Access UI Control Contain by Data Template.

  • Create a `bool` field in your collection , then bind the visibility to it using a [BooleanToVisibilityConverter](http://www.codeproject.com/Articles/758656/Use-Converters-in-your-Windows-Phone-Apps) `Visibility="{Binding ShowImgReceiver, Converter={StaticResource BooleanToVisibilityConverter}}"` – Barnstokkr Mar 03 '15 at 12:18
  • Hello Barnstokkt, I am Beginner so It's might be possible i am Wrong. Is this Possible To do This Without using Any Class..??? – Chirag Solanki Mar 03 '15 at 13:13
  • I don't understand what you mean by "Without using Any Class" everything in C# is a class (except technically void) – Barnstokkr Mar 03 '15 at 14:56
  • Simple Model Class. i am Using MVVM Architecture. – Chirag Solanki Mar 03 '15 at 15:19

3 Answers3

1
 <ListView x:Name="lst1" Grid.ColumnSpan="2"  >

        <ListView.ItemTemplate>
        <DataTemplate >
                <Grid>

                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>

                    <Image  Visibility="{Binding SenderVisibility}" x:Name="imgSender" Source="Assets/button_register.png"  Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="8,15,8,8" />
                        <TextBlock  Text="{Binding Sender}" FontSize="18" Foreground="Black" FontWeight="Bold" HorizontalAlignment="Center" Margin="0,12,0,0"/>

                    <Image x:Name="imgReceiver" Source="Assets/button_register.png"  Grid.Row="1" Stretch="None" VerticalAlignment="Top" Margin="4,0" />
                    <TextBlock Text="{Binding Receiver}"  FontSize="18" Foreground="Black" FontWeight="Bold" HorizontalAlignment="Center" Grid.Row="1"  VerticalAlignment="Top"/>
                    <Image Source="Assets/scroll_line_addcategory.png" Grid.Row="2" Stretch="None" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="4,8,4,0" />
                </Grid>


            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

And my Model Class is public class User : INotifyPropertyChanged { private string sender = string.Empty; private string receiver = string.Empty; private string senderVisibility = string.Empty; private string receiverVisibility = string.Empty; public string Sender { get { return sender; } set { if (value != this.sender) { this.sender = value; NotifyPropertyChanged("Sender"); } } }

    public string Receiver
    {
        get
        {
            return this.receiver;
        }
        set
        {
            if (value != this.receiver)
            {
                this.receiver = value;
                NotifyPropertyChanged("Receiver");
            }
        }
    }
    public string ReceiverVisibility
    {
        get
        {
            return this.receiverVisibility;
        }
        set
        {
            if (value != this.receiverVisibility)
            {
                this.receiverVisibility = value;
                NotifyPropertyChanged("ReceiverVisibility");
            }
        }
    }
    public string SenderVisibility
    {
        get
        {
            return this.senderVisibility;
        }
        set
        {
            if (value != this.senderVisibility)
            {
                this.senderVisibility = value;
                NotifyPropertyChanged("SenderVisibility");
            }
        }
    }

    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
Justin CI
  • 2,693
  • 1
  • 16
  • 34
  • It's Not Working justin. – Chirag Solanki Mar 03 '15 at 13:14
  • do you have a viewmodel for XAMl class? – Justin CI Mar 03 '15 at 13:16
  • Ya I have Model Class.If i am use Model Class Than It's Properly Work.but my Question i That If I Don't Want To use Model Class And Access the Property of ImgSender in Xaml.cs Class.Is this Possible..??? – Chirag Solanki Mar 03 '15 at 13:29
  • In Xaml.cs access the Model class by (this.DataContext as ClassViewModel).GridViewModel – Justin CI Mar 03 '15 at 13:31
  • Do you have any Referance Demo or Link..?? – Chirag Solanki Mar 03 '15 at 13:36
  • If you are that particular you can use ContentPresenter contentPresenter = FindVisualChild(yourListBoxItem);DataTemplate yourDataTemplate = contentPresenter.ContentTemplate;Image img = yourDataTemplate.FindName("imgSender", contentPresenter) – Justin CI Mar 03 '15 at 13:37
  • http://stackoverflow.com/questions/19379946/how-to-access-a-control-within-data-template-from-code-behind – Justin CI Mar 03 '15 at 13:37
  • I m Sorry Guys it's not properly implemented in My Code May Be it's My Mistake.I Will try at my Best.If I got Solution than I am Telling you. And Thank You Very Much Justin – Chirag Solanki Mar 03 '15 at 13:55
0

If your ListView contains only one item you can easily subscribe to Loaded event of imgSender and then control it's Visibility.

private Image _imgSender;

void Image_Loaded(object sender, RoutedEventArgs e)
{
     _imgSender = sender as Image();
}

void AnotherMethod()
{
      if (_imgSender != null)
         _imgSender.Visibility = Visibility.Collapsed;
}

EDIT FOR MULTIPLE ITEMS

void HideImage(int elementIndex)
{
    var container = lst1.ContainerFromIndex(elementIndex) as ListViewItem;

   var imageSender = (container.Content as Grid).Children[0] as Image;
   imageSender.Visibility = Visibility.Collapsed;
}
khamitimur
  • 1,078
  • 13
  • 22
0

You can add a BooleanToVisibilityConverter

<Page.Resources>
    <converter:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Page.Resources>

<ListView x:Name="lst1" Grid.ColumnSpan="2"  
    ItemsSource="{Binding TelecomCollection}">
    <ListView.ItemTemplate>
    <DataTemplate >
            <Grid>

                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <Image x:Name="imgSender" Source="Assets/button_register.png"  Stretch="None" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="8,15,8,8" 
                      Visibility="{Binding ShowImgReceiver, Converter={StaticResource BooleanToVisibilityConverter}}"/>
                    <TextBlock  Text="{Binding Sender}" FontSize="18" Foreground="Black" FontWeight="Bold" HorizontalAlignment="Center" Margin="0,12,0,0"/>

                <Image x:Name="imgReceiver" Source="Assets/button_register.png"  Grid.Row="1" Stretch="None" VerticalAlignment="Top" Margin="4,0" />
                <TextBlock Text="{Binding Receiver}"  FontSize="18" Foreground="Black" FontWeight="Bold" HorizontalAlignment="Center" Grid.Row="1"  VerticalAlignment="Top"/>
                <Image Source="Assets/scroll_line_addcategory.png" Grid.Row="2" Stretch="None" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="4,8,4,0" />
            </Grid>


        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Then in your item class you can add ShowImgReceiver

public class Telecom
{
     string Receiver {get; set;}
     string Sender {get; set;}
     bool ShowImgReceiver{get; set;}
} 

In your binding class

public class ViewModel
{
    public ObservableCollection<Telecom> TelecomCollection {get; set;}
}

If you want to make an item's image visibility Collapsed, then

 TelecomCollection[0].ShowImgReceiver = false;

and to make it again Visible

 TelecomCollection[0].ShowImgReceiver = true;
Barnstokkr
  • 2,904
  • 1
  • 19
  • 34