Xaml WPF:在列表框中通过触摸更改颜色

Xaml WPF:在列表框中通过触摸更改颜色,xaml,windows-phone,Xaml,Windows Phone,我想更改列表中某个项目被触摸时的背景色。我可以用XAML做这个吗 <ListBox x:Name="lstFlags"> <ListBox.ItemTemplate> <DataTemplate> <Grid> ... </Grid>

我想更改列表中某个项目被触摸时的背景色。我可以用XAML做这个吗

        <ListBox x:Name="lstFlags">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                         ...
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>

            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>

...

您可以将
鼠标向下
鼠标向下
事件添加到
网格
,但这不会告诉您选择了哪个项目。您需要的是捕获selection changed事件并更改所选项目的颜色

另外,与其只是在选择时更改颜色,最好在数据项中放置一个标志,告诉您是否触摸了它们,然后根据该标志的值更新XAML中的颜色

假设我们有这个视图模型:

public partial class MainWindow : Window
{
    public class Item : INotifyPropertyChanged
    {
        public string ItemText { get; set; }

        private bool _wasTouched;
        public bool WasTouched
        {
            get { return _wasTouched; }
            set { _wasTouched = value; OnPropertyChanged( "WasTouched" ); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged( string name )
        {
            var handler = PropertyChanged;
            if( handler != null ) handler( this, new PropertyChangedEventArgs( name ) );
        }
    }

    public ObservableCollection<Item> Items { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        Items = new ObservableCollection<Item>();
        Items.Add( new Item() { ItemText = "ugh." } );
        Items.Add( new Item() { ItemText = "whee" } );
        Items.Add( new Item() { ItemText = "nee" } );
        Items.Add( new Item() { ItemText = "ping" } );
        Items.Add( new Item() { ItemText = "neeeuuwwwop" } );

        this.DataContext = this;
    }

    private void ListBox_SelectionChanged( object sender, System.Windows.Controls.SelectionChangedEventArgs e )
    {
        ((sender as ListBox).SelectedItem as Item).WasTouched = true;
    }
}
在XAML中,我们告诉
ListBox
关于项目和选择更改处理程序的信息。我们告诉它使用带有项目文本的
TextBox
显示每个项目。然后我们修改您已经提供的样式。。。我们通常将项目的背景设置为绿色,但在
waspothed
上添加一个
DataTrigger
,将背景更改为红色


还有一个问题:列表使用蓝色背景绘制所选项目,这会覆盖您的背景色。这就是
Style.Resources
部分的作用-它告诉XAML使用红色背景绘制所选项目。

您可以添加onClick事件,并更改viewmodel中的颜色?在哪个标记中添加onClick?您希望命中目标的标记。可能是网格?在类型“网格”中找不到属性“onClick”。啊,是的,mousedown和mouseup。在类型“网格”中找不到属性“SelectionChanged”,请仔细阅读。
SelectionChanged
事件在
ListBox
上。
<ListBox ItemsSource="{Binding Items}" SelectionChanged="ListBox_SelectionChanged" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <TextBlock Text="{Binding ItemText}"/>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>

    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red" />
            </Style.Resources>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="Background" Value="Green"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding WasTouched}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>