1

我在 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;
    }

请帮忙..!!

4

2 回答 2

2

由于您已将 绑定Image到对象的Image属性,因此settings只需更改那里的路径 ( settings.Image = "Images/newImage.png"),列表应该会获取对图像路径的更改。

为此,请订阅OnMouseLeftButtonDownorOnMouseLeftButtonUp事件。

编辑 您也可以将 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>

你如何使用它:

 <Image Source="{Binding OnOff, Converter={StaticResource OnOff}}" Width="100" Height="40" />

您的设置类:

public class Settings : INotifyPropertyChanged  
{
    public Settings(string name, string image, bool onOff)
    {
        Name = name;
        Image = new BitmapImage (new Uri(image, UriKind.Absolute));
        OnOff = onOff;
    }

    public string Name { get; set; }
    public ImageSource Image { get; set; }

    private bool m_onOff;
    public bool OnOff 
        {
        get { return m_onOff; }
        set { 
            m_onOff = value;
            if (PropertyChanged != null)
                {
                PropertyChanged (this, new PropertyChangedEventArgs ("OnOff"));
                }
            } 
        }

    public event PropertyChangedEventHandler PropertyChanged;
}

我认为您需要实现 INotifyPropertyChanged 接口来通知列表有关基础类实例中更改的属性的信息。此外,如果您打算在绑定后将其他元素添加到列表中,则应该将其作为 ObservableCollection。

我已经通过订阅 SelectionChanged 事件进行了测试。但是,由于我没有在移动设备上开发,所以我不知道任何点击选项,也许您可​​以使用 Image_Tap 事件。

    listBox1.SelectionChanged += new SelectionChangedEventHandler (listBox1_SelectionChanged);


void listBox1_SelectionChanged (object sender, SelectionChangedEventArgs e)
    {
        foreach (Settings item in e.AddedItems)
        {
            item.OnOff = !item.OnOff;
        }
    }
于 2012-11-15T05:37:49.750 回答
2

一个快速的解决方案是,

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));
        }
    }
于 2012-11-15T06:24:57.530 回答