C# 单击WP7时更改列表框项目

C# 单击WP7时更改列表框项目,c#,windows-phone-7,xaml,C#,Windows Phone 7,Xaml,我在WP7中有一个绑定的列表框,每个部分包含一组项目,我想在单击时更改图标的路径,以便更改单击的图标。 这是我的XAML数据,需要在单击按钮上更改斜体和粗体的Stackpanel <ListBox Height="768" HorizontalAlignment="Left" Name="listBox1" Margin="0,0,0,0" VerticalAlignment="Top" Width="480" Grid.RowSpan="2" >

我在WP7中有一个绑定的列表框,每个部分包含一组项目,我想在单击时更改图标的路径,以便更改单击的图标。 这是我的XAML数据,需要在单击按钮上更改斜体和粗体的Stackpanel

   <ListBox Height="768" HorizontalAlignment="Left" Name="listBox1" Margin="0,0,0,0" 
            VerticalAlignment="Top" Width="480" Grid.RowSpan="2" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Button BorderBrush="Black" Width="460" Height="100">
                    <Button.Content>
                        <StackPanel Orientation="Horizontal" Height="80" Width="400">
                            <Image Source="{Binding Image}" Width="80" Height="50"/>
                            <StackPanel Orientation="Vertical" Height="80">
                                <StackPanel Orientation="Horizontal" Height="40">
                                    <TextBlock Width="200" FontSize="28" Text="{Binding Name}" Height="50"/>
                                    <StackPanel Orientation="Horizontal">
                                        ***<Image Source="{Binding OnOff}" Width="100" Height="40" Tap="Image_Tap"/>***
                                        </StackPanel>
                                </StackPanel>
                            </StackPanel>
                        </StackPanel>
                    </Button.Content>
                </Button>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

******
我用这种方式填写了这个列表框

     void MainPage_Loaded(object sender, RoutedEventArgs e)
     {
        List<settings> settingsData = new List<settings>();

        settingsData.Add(new settings("Administrator","Images/ad.png",""));
        settingsData.Add(new settings("Wi-Fi","Images/wifi.png","Images/off.png"));
        settingsData.Add(new settings("Bluetooth", "Images/BlueTooth.png", "Images/On.png"));
        settingsData.Add(new settings("Airplane Mode", "Images/747.png", "Images/off.png"));
        settingsData.Add(new settings("VPN", "", ""));
        settingsData.Add(new settings("Hotspot", "", "Images/unchecked.png"));
        settingsData.Add(new settings("NFC", "", "Images/checked.png"));
        settingsData.Add(new settings("Sound","Images/sound.png",""));
        settingsData.Add(new settings("Display","Images/display.png",""));

        listBox1.ItemsSource = settingsData;
    }
void主页\u已加载(对象发送方,路由目标)
{
列表设置数据=新列表();
setingsdata.Add(新设置(“管理员”、“图像/ad.png”和“));
设置数据添加(新设置(“Wi-Fi”、“Images/wifi.png”、“Images/off.png”);
setingsdata.Add(新设置(“Bluetooth”、“Images/Bluetooth.png”、“Images/On.png”);
添加(新设置(“飞机模式”、“图像/747.png”、“图像/off.png”);
设置数据添加(新设置(“VPN”、“VPN”、“VPN”、“VPN”);
setingsdata.Add(新设置(“热点”、“图像/未选中的.png”);
setingsdata.Add(新设置(“NFC”、“图像/checked.png”);
setingsdata.Add(新设置(“声音”、“图像/Sound.png”和“));
setingsdata.Add(新设置(“Display”、“Images/Display.png”和“));
listBox1.ItemsSource=设置数据;
}

救命啊

既然您已将
图像
绑定到
设置
对象的
图像
属性,只需更改该对象的路径(
settings.Image=“Images/newImage.png”
),列表就会显示对图像路径的更改

为此,请订阅
OnMouseLeftButtonDown
OnMouseLeftButtonUp
事件

编辑 您也可以将OnOff属性设置为bool类型,并为此提供转换器。 通常,所有项目的打开和关闭图标都相同(提供UI一致性)。以下是转换器类:

public class OnOffImageConverter : IValueConverter
    {
    public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
        {
        if ((bool)value)
            {
            return new BitmapImage (new Uri ("Images/On.png", UriKind.Relative));
            }
        else
            {
            return new BitmapImage (new Uri ("Images/Off.png", UriKind.Relative));
            }
        }

    public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture)
        {
        throw new NotImplementedException ();
        }
    }
在xaml中为转换器定义静态资源:

<UserControl.Resources>
    <instance:OnOffImageConverter x:Key="OnOff" />
</UserControl.Resources>
我认为您需要实现INotifyPropertyChanged接口来通知列表底层类实例中已更改的属性。此外,如果您计划在绑定列表后向列表中添加其他元素,则应该将其作为可观察集合

我已通过订阅SelectionChanged事件进行了测试。然而,由于我没有在手机上开发,我不知道任何点击选项,也许你可以使用Image\u tap event

    listBox1.SelectionChanged += new SelectionChangedEventHandler (listBox1_SelectionChanged);


void listBox1_SelectionChanged (object sender, SelectionChangedEventArgs e)
    {
        foreach (Settings item in e.AddedItems)
        {
            item.OnOff = !item.OnOff;
        }
    }

由于您已将
图像
绑定到
设置
对象的
图像
属性,因此只需更改其中的路径(
settings.Image=“Images/newImage.png”
),列表就会显示对图像路径的更改

为此,请订阅
OnMouseLeftButtonDown
OnMouseLeftButtonUp
事件

编辑 您也可以将OnOff属性设置为bool类型,并为此提供转换器。 通常,所有项目的打开和关闭图标都相同(提供UI一致性)。以下是转换器类:

public class OnOffImageConverter : IValueConverter
    {
    public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
        {
        if ((bool)value)
            {
            return new BitmapImage (new Uri ("Images/On.png", UriKind.Relative));
            }
        else
            {
            return new BitmapImage (new Uri ("Images/Off.png", UriKind.Relative));
            }
        }

    public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture)
        {
        throw new NotImplementedException ();
        }
    }
在xaml中为转换器定义静态资源:

<UserControl.Resources>
    <instance:OnOffImageConverter x:Key="OnOff" />
</UserControl.Resources>
我认为您需要实现INotifyPropertyChanged接口来通知列表底层类实例中已更改的属性。此外,如果您计划在绑定列表后向列表中添加其他元素,则应该将其作为可观察集合

我已通过订阅SelectionChanged事件进行了测试。然而,由于我没有在手机上开发,我不知道任何点击选项,也许你可以使用Image\u tap event

    listBox1.SelectionChanged += new SelectionChangedEventHandler (listBox1_SelectionChanged);


void listBox1_SelectionChanged (object sender, SelectionChangedEventArgs e)
    {
        foreach (Settings item in e.AddedItems)
        {
            item.OnOff = !item.OnOff;
        }
    }
一个快速的解决办法是

private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        var imageSource = ((sender as Image).Source as BitmapImage).UriSource.OriginalString;
        if (imageSource.Contains("On"))
        {
            (sender as Image).Source = new BitmapImage(new Uri("Images/Off.png", UriKind.Relative));
        }
        else
        {
            (sender as Image).Source = new BitmapImage(new Uri("Images/On.png", UriKind.Relative));
        }
    }
一个快速的解决办法是

private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        var imageSource = ((sender as Image).Source as BitmapImage).UriSource.OriginalString;
        if (imageSource.Contains("On"))
        {
            (sender as Image).Source = new BitmapImage(new Uri("Images/Off.png", UriKind.Relative));
        }
        else
        {
            (sender as Image).Source = new BitmapImage(new Uri("Images/On.png", UriKind.Relative));
        }
    }

我需要在on和off之间切换,所以我首先需要检查图像是on.png还是off.png,然后我需要相应地更改它以切换。如何在OnMouseLeftButtonDown事件中进行检查?我需要在on和off之间切换,因此首先需要检查图像是on.png还是off.png,然后我需要相应地更改它以进行切换。我如何在OnMouseLeftButtonDown事件中检查它?它确实有效。刚删除了图像前的“/”。非常感谢……)它确实奏效了。刚删除了图像前的“/”。非常感谢……)