单击WP7上的列表框项目更改

时间:2012-11-15 05:18:07

标签: c# windows-phone-7 xaml

我在WP7中有一个绑定列表框,其中包含每个部分中的一组项目,我想在单击时更改图标的路径,以便更改单击的图标。 这是我的XAML数据,需要在点击按钮上更改Italic和Bold中的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;
    }

请帮助.. !!

2 个答案:

答案 0 :(得分:2)

由于您已将Image绑定到Image对象的settings属性,只需更改其中的路径(settings.Image = "Images/newImage.png"),列表应将更改添加到OnMouseLeftButtonDown图像路径。

为此,请订阅OnMouseLeftButtonUppublic 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 (); } } 活动。

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

<UserControl.Resources>
    <instance:OnOffImageConverter x:Key="OnOff" />
</UserControl.Resources>

在xaml中为转换器定义静态资源:

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

您的设置类:

    listBox1.SelectionChanged += new SelectionChangedEventHandler (listBox1_SelectionChanged);


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

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

我已经测试了订阅SelectionChanged事件。但是,由于我没有在移动设备上开发,我不知道任何分接选项,也许你可以使用Image_Tap事件。

{{1}}

答案 1 :(得分: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));
        }
    }