访问和编辑按钮上的图像按钮单击后面的代码

时间:2016-04-13 07:21:41

标签: c# xaml win-universal-app

我有一个像这样构建的Listview:

<ListView x:Name="listprimi" RelativePanel.Below="primi" ItemsSource="{x:Bind obs_prims2}" HorizontalAlignment="Center" VerticalAlignment="Center">
    <ListView.ItemTemplate>
         <DataTemplate>
             <StackPanel Orientation="Horizontal">
                  <Button Tag="{Binding id}" Grid.Column="0" Padding="0" BorderThickness="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="100" Background="White" Click="selectMeal0">
                      <Image Name="sel0" Width="80" Height="80" Source="Images/ic_uncheck.png" RelativePanel.AlignLeftWithPanel="True" />
                  </Button>
                  <Image Width="120" Height="120" Margin="30,0,0,0" Source="{Binding idImg}" RelativePanel.AlignLeftWithPanel="True" />
                  <TextBlock Text="{Binding descr}" RelativePanel.AlignHorizontalCenterWithPanel="True" Height="100" Name="namemeal" Padding="60,15" Foreground="Gray" FontSize="26.7"/>
             </StackPanel>
         </DataTemplate>
     </ListView.ItemTemplate>
 </ListView>

我想在单击按钮时更改名为“sel0”的图像,该图像位于按钮内部。我已经设置了函数selectMeal0的代码,但我不知道如何做到这一点。 另外,我还想在同一个函数中更改列表中所有其他元素的图像。 我尝试了类似this的内容,但它不起作用。

更新: 这是班级

public class Pasto : INotifyPropertyChanged
{
    public string id { get; set; }
    public string descr { get; set; }
    public ImageSource idImg { get; set; }
    private ImageSource imgChecked;
    public ImageSource ImgChecked {
        get { return imgChecked; }
        set
        {
            if (imgChecked != value)
            {
                imgChecked = value;
                OnPropertyChanged("ImgChecked");
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

我已经改变了ListView:

<ListView x:Name="listprimi" RelativePanel.Below="primi" ItemsSource="{x:Bind obs_prims2}" HorizontalAlignment="Center" VerticalAlignment="Center">
    <ListView.ItemTemplate>
         <DataTemplate>
             <StackPanel Orientation="Horizontal">
                  <Button Tag="{Binding id}" Grid.Column="0" Padding="0" BorderThickness="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="100" Background="White" Click="selectMeal0">
                      <Image Name="sel0" Width="80" Height="80" Source="{Binding ImgChecked}" RelativePanel.AlignLeftWithPanel="True" />
                  </Button>
                  <Image Width="120" Height="120" Margin="30,0,0,0" Source="{Binding idImg}" RelativePanel.AlignLeftWithPanel="True" />
                  <TextBlock Text="{Binding descr}" RelativePanel.AlignHorizontalCenterWithPanel="True" Height="100" Name="namemeal" Padding="60,15" Foreground="Gray" FontSize="26.7"/>
             </StackPanel>
         </DataTemplate>
     </ListView.ItemTemplate>
 </ListView>

所以我要编辑的图像是课堂上的ImageChecked。 函数selectMeal应将所有图像更改为“未选中”,然后将所选项目的图像更改为“已选中”。

2 个答案:

答案 0 :(得分:0)

selectMeal0

,您可以访问发件人对象,在这种情况下为Button。 Button具有Content属性,该属性又包含Image。此时,您可以对图像执行任何操作。

<强> BUT

您还可以将图像源绑定到模型的属性。并更改模型以更新图像。

答案 1 :(得分:0)

要在Image的{​​{1}}内更改DataTemplate,您应该在按钮点击时更改收藏中的必要项目:

因此,ListView的{​​{1}}集合为obs_prims2。然后,您需要从集合ItemsSource中获取一个项目。例如:

ListView

然后设置新的图像地址:

obs_prims2

此外,您的Model类应实现var item=obs_prims2.FirstOrDefault(); 接口以显示任何更改。例如:

item.idImg="C:/1.png";

<强>更新

要获得所需的项目,您可以按索引获取项目,就像那样 INotifyPropertyChanged。例如:

public class Book : INotifyPropertyChanged
{
    private string title;
    public string Title {
       get { return title; }
       set {
           title = value;
           OnPropertyChanged("Title");
       }         
    }    

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(String info) 
    {
       if (PropertyChanged != null) {
           PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
}

<强>型号:

Person person = Persons[1];
相关问题