【问题标题】:Access and edit Image inside Button on Button click in code behind在后面的代码中单击按钮上的按钮访问和编辑按钮内的图像
【发布时间】:2016-08-04 03:06:37
【问题描述】:

我有一个这样构建的 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 应该将所有图像更改为“未选中”,然后将所选项目的图像更改为“选中”。

【问题讨论】:

标签: c# xaml win-universal-app


【解决方案1】:

selectMeal0 中,您可以访问发送者对象,在本例中为Button。 Button 具有Content 属性,该属性又包含Image。此时您可以对 Image 进行任何操作。

但是

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

【讨论】:

    【解决方案2】:

    为了在DataTemplateListView 中更改Image,您应该在单击按钮时从您的收藏中更改必要的项目:

    所以,您有一个集合 obs_prims2ItemsSourceListView。然后您需要从您的收藏中取出一件物品obs_prims2。例如:

    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];。例如:

    private void FillDataGridByTypeCollection()
    {
        Persons.Add(new Person() { Id = 1, Name = "Ben" });
        Persons.Add(new Person() { Id = 1, Name = "Joseph" });
        Persons.Add(new Person() { Id = 1, Name = "Jon" });
        Persons.Add(new Person() { Id = 1, Name = "Magnus Montin" });
        Persons.Add(new Person() { Id = 1, Name = "Andy" });
        Person person = Persons[1];
    }
    
    private ObservableCollection<Person> persons = new ObservableCollection<Person>();
    public ObservableCollection<Person> Persons
    {
       get { return persons; }
       set { persons = value; }
    }
    

    型号:

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    

    【讨论】:

    • 由于项目源中的图像不是我想要更改的图像,我认为这不是我想要的。不过还是谢谢你。
    • @Janinho67 不仅可以更改Image,还可以更改模型中的任何项目。这只是一个如何编辑项目的示例。这是更改模型的常用方法。
    • 我正在尝试使用您的建议。开始看到一些结果。我想问你一件事。如果我想选择一个不是第一个的特定元素,我该怎么做?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 2016-03-02
    • 2021-03-27
    • 2021-03-13
    • 1970-01-01
    • 2016-08-27
    相关资源
    最近更新 更多