Reputation: 7
I have many bool and string need to binding. So I write a IMultiValueConverter and binding like this:
private ObservableCollection<bool> ischeckeds;
public ObservableCollection<bool> IsCheckeds
{
get { return ischeckeds; }
set { Set(ref ischeckeds, value); } //MvvmLight
}
class IndexToItemConverter:ImultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length ==2 )
{
if (values[0] is IList list )
{
int index;
if (int.TryParse((string)values[1],out index)&& index < list.Count && index>=0)
{
return list[index];
}
}
}
return null;
}
...
}
<local:IndexToItemConverter x:Key="indexToitemConverter" />
<RadioButton>
<RadioButton.IsChecked>
<MultiBinding Converter="{StaticResource indexToitemConverter}">
<Binding Path="IsCheckeds" />
<Binding Path="Tag" RelativeSource="{RelativeSource Mode=Self}" />
</MultiBinding>
</RadioButton.IsChecked>
</RadioButton>
SomeOne told me I have to use class to load the bool value type and implement INotifyPropertyChanged,like:
class Boolitem : ObservableObject //MvvmLight
{
private bool ischecked;
public bool IsChecked
{
get { return ischecked;}
set { Set(ref ischecked;value); } //MvvmLight
}
}
But if I use this way, My IndexToItemConverter couldn't be reused in (the Collection I don't need to NotifyPropertyChanged). cause it is binding in isCheck Porperty I have to take it in IndexToItemConverter
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
...
return list[index].isChecked;
...
}
has a better ,more elegant anyone ? Thanks in advance
Upvotes: -1
Views: 51