![](/img/trans.png)
[英]click on a button and inside a button to raise another button to be clicked with code behind?
[英]Access and edit Image inside Button on Button click in code behind
我有一個像這樣構建的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的代碼,但我不知道如何做到這一點。 另外,我還想在同一個函數中更改列表中所有其他元素的圖像。 我試圖像這樣 ,但它不工作。
更新:這是班級
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應將所有圖像更改為“未選中”,然后將所選項目的圖像更改為“已選中”。
在selectMeal0
,您可以訪問sender對象,在這種情況下是一個Button
。 Button具有Content
屬性,該屬性又包含Image
。 此時,您可以對圖像執行任何操作。
但
您還可以將圖像源綁定到模型的屬性。 並更改模型以更新圖像。
要在ListView
的DataTemplate
中更改Image
,您應該在按鈕單擊時更改集合中的必要項:
因此,您有一個集合obs_prims2
作為ListView
ItemsSource
。 然后你需要從你的集合obs_prims2
一個項目。 例如:
var item=obs_prims2.FirstOrDefault();
然后設置新的圖像地址:
item.idImg="C:/1.png";
而且,您的Model類應該實現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; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.