在运行时访问DataTemplate控件

时间:2015-03-03 12:07:07

标签: c# xaml windows-phone-8.1

我正在使用Windows Phone 8.1。我正在制作包含两个图像和两个TextBlock的简单ListView演示。

     <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>

我的问题是我想要设定&#39; imgSender&#39;在Xaml.cs文件中的运行时。 Anybuddy是否对数据模板包含的访问UI控件有任何想法。

3 个答案:

答案 0 :(得分: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>

我的模型类是  公共类用户:INotifyPropertyChanged     {         private string sender = string.Empty;         private string receiver = string.Empty;         private string senderVisibility = string.Empty;         private string receiverVisibility = string.Empty;         公共字符串发件人         {             得到             {                 退货发件人;             }             组             {                 if(value!= this.sender)                 {                     this.sender = value;                     NotifyPropertyChanged(&#34;发件人&#34);                 }             }         }

    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;

答案 1 :(得分:0)

如果您的ListView只包含一个项目,则可以轻松订阅Loaded imgSender事件,然后将其控制为Visibility

private Image _imgSender;

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

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

修改 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;
}

答案 2 :(得分:0)

您可以添加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>

然后在您的项目类中,您可以添加ShowImgReceiver

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

在你的装订课程中

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

如果您想要制作商品的图片可见度Collapsed,那么

 TelecomCollection[0].ShowImgReceiver = false;

并重新制作Visible

 TelecomCollection[0].ShowImgReceiver = true;