I making MVVM application.
I want to Bind SelectedPointMarkers of DataPointSelectionModifier.
But SelectedPointMarkers is readonly property.
Therefore, I couldn’t bind.
How can I bind it?
- Kenichi Kobayashi asked 5 years ago
- You must login to post comments
I think our DataPointSelectionModifier example binds to this property – but we bind to an existing collection.
Look here:
WPF Chart Data-Point Selection
and in the code…
<ListBox ItemsSource="{Binding Source={x:Reference Name=PointMarkersSelectionModifier}, Path=SelectedPointMarkers}">
Best regards
Andrew
- Andrew Burnett-Thompson answered 5 years ago
- You must login to post comments
Thank you for your reply.
How to ask questions was not good.
We understand that Bind between controls in View is possible.
I want to Bind to the property of ViewModel, is it possible?
- Kenichi Kobayashi answered 5 years ago
-
No it won’t work as its a readonly property as you said. The best thing you can do is to create an attached behaviour that passes the value to your ViewModel.
- You must login to post comments
It was possible to solve by inheriting DataPointSelectionModifier and creating your own DependencyProperty for SelectedPointMarkers2.
Thanks a lot.
public class DataPointSelectionModifierExtention : DataPointSelectionModifier
{
public ObservableCollection<DataPointInfo> SelectedPointMarkers2
{
get => (ObservableCollection<DataPointInfo>)GetValue(SelectedPointMakers2Property);
set => SetValue(SelectedPointMakers2Property, value);
}
public static readonly DependencyProperty SelectedPointMakers2Property =
DependencyProperty.Register(
nameof(SelectedPointMarkers2),
typeof(ObservableCollection<DataPointInfo>),
typeof(DataPointSelectionModifierExtention),
new PropertyMetadata(new ObservableCollection<DataPointInfo>()));
public DataPointSelectionModifierExtention()
{
base.SelectedPointMarkers.CollectionChanged += SelectedPointMakers_CollectionChanged;
}
private void SelectedPointMakers_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
foreach (var item in e.NewItems.Cast<DataPointInfo>())
{
this.SelectedPointMarkers2.Add(item);
}
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
SelectedPointMarkers2.Clear();
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
foreach (var item in e.OldItems.Cast<DataPointInfo>())
{
this.SelectedPointMarkers2.Remove(item);
}
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
default:
throw new ApplicationException();
}
}
}
- Kenichi Kobayashi answered 5 years ago
- You must login to post comments
Please login first to submit.