По вашему требованию, лучший способ - это свойство Foreground
кнопки привязки со значением Boolean
, затем использовать IValueConverter
для преобразования его в SolidColorBrush
.Вы можете изменить цвет символа, изменив значение IsFav
в методе нажатия кнопки.
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
public List<Item> Items { get; set; }
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
Items = new List<Item>();
Items.Add(new Item { Name = "Nice Day", IsFav = true });
Items.Add(new Item { Name = "Nice Day", IsFav = false });
Items.Add(new Item { Name = "Nice Day", IsFav = false });
}
private void AddToFevBtn_Click(object sender, RoutedEventArgs e)
{
var btn = sender as Button;
var item = btn.DataContext as Item;
item.IsFav = !item.IsFav;
}
}
public class Item : INotifyPropertyChanged
{
public bool IsFav
{
get
{
return isFav;
}
set
{
isFav = value;
OnPropertyChanged();
}
}
private bool isFav;
public string Name { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
// Raise the PropertyChanged event, passing the name of the property whose value has changed.
if (PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class ColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (Boolean.Parse(value.ToString()))
{
return new SolidColorBrush(Colors.Red);
}
else
{
return new SolidColorBrush(Colors.Black);
}
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
Xaml
<Page.Resources>
<local:ColorConverter x:Key="Converter"/>
</Page.Resources>
<Grid>
<GridView ItemsSource="{x:Bind Items}">
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Width="100" >
<TextBlock Text="{Binding Name}"
TextAlignment="Center"
HorizontalAlignment="Center"
Margin="0,10,0,0"/>
<Button Style="{StaticResource TextBlockButtonStyle}"
HorizontalAlignment="Center"
Margin="0,30,0,0"
Content=""
FontSize="25"
FontFamily="Segoe MDL2 Assets"
Foreground="{Binding IsFav,Converter={StaticResource Converter}}"
Click="AddToFevBtn_Click"
x:Name="addToFevBtn"
/>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>