Ответ 1
Вы можете реализовать INotifyPropertyChanged
для своей модели и добавить
public class Model : INotifyPropertyChanged
{
private bool _isSelected;
public string PHOTO { get; set; }
public string NAME { get; set; }
public string DISTANCE { get; set; }
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
if (value != _isSelected)
{
_isSelected = value;
RaisePropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
var propertyChanged = Volatile.Read(ref PropertyChanged);
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Итак, в OwnerList_SelectionChanged
вы должны изменить это свойство
private void OwnerList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems != null && e.AddedItems.Count > 0)
{
var addedItem = e.AddedItems.Cast<Model>().ToList();
foreach(var item in addedItem)
{
item.IsSelected = true;
}
}
if (e.RemovedItems != null && e.RemovedItems.Count > 0)
{
var removedItems = e.RemovedItems.Cast<Model>().ToList();
foreach (var item in removedItems)
{
item.IsSelected = false;
}
}
}
Создайте простой конвертер для Stroke
public class EllipseStrokeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var result = new SolidColorBrush(Colors.Blue);
if ((bool)value)
{
result = new SolidColorBrush(Colors.Red);
}
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
и использовать его в шаблоне
<local:EllipseStrokeConverter x:Key="EllipseStrokeConverter"/>
<DataTemplate x:Key="OwnerListTemplate">
<StackPanel Margin="20,0,20,0">
<Ellipse Height="120"
Width="120"
Margin="4"
Stroke="{Binding IsSelected, Converter={StaticResource EllipseStrokeConverter}}"
StrokeThickness="2">
<Ellipse.Fill>
<ImageBrush ImageSource="{Binding PHOTO, Converter={StaticResource Imageconverter}}"/>
</Ellipse.Fill>
</Ellipse>
<TextBlock x:Name="OwnerName"
Text="{Binding NAME}"
FontSize="22"
Foreground="Gray"
FontWeight="Bold"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
<TextBlock x:Name="distance"
Text="{Binding DISTANCE}"
FontSize="20"
Foreground="Gray"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
UPDATE
Я знаю другое решение без манипуляции C#
. В нашем ListBoxItem
мы выберем некоторое свойство из ContentControl
, которое будет описывать логику для нашего свойства в DataTemplate
и использовать это значение proteprty через Binding
.
<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver"/>
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimation Duration="0" To=".5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentContainer"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentContainer"
Storyboard.TargetProperty="BorderBrush">
<DiscreteObjectKeyFrame KeyTime="0" Value="Yellow"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentControl x:Name="ContentContainer"
BorderBrush="Blue"
ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}"
Foreground="{TemplateBinding Foreground}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Посмотрите на Selected
визуальное состояние, где мы изменим значение свойства BorderBrush
, которое мы используем в DataTemplate
таким образом
Stroke="{Binding BorderBrush, ElementName=ContentContainer}"