I have two ListBoxes and each of them have a TextBlock inside (from a DataTemplate). I have two buttons to transfer the text from one list box to the other, and vice versa.
I also allow the user to drag and drop to move text from one ListBox to another, which functionally works fine.
However for my buttons, I use "Style="{Binding
IsInGroup, Converter={StaticResource StyleFunctionConverter}}"
and when the selected button appears on the other list on button click then it changes the text style and color, but when I try to transfer the text using drag and drop it just
simply transfers the text and does not change text color.
My code:
<UserControl.Resources><local:StyleFunctionConverter x:Key="StyleFunctionConverter" /></UserControl.Resources><Grid x:Name="LayoutRoot" Background="{StaticResource BGBrush_1}"><Grid.ColumnDefinitions><ColumnDefinition Width="Auto" /><ColumnDefinition/></Grid.ColumnDefinitions><Grid Width="200"><Grid.RowDefinitions><RowDefinition/><RowDefinition Height="Auto" /></Grid.RowDefinitions> //First ListBox each ListBox inside scrollviewer<ScrollViewer Grid.Column="1" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" Style="{StaticResource MP_ScrollViewerStyle}" BorderBrush="{x:Null}"><Grid Margin="40,20"><Grid.RowDefinitions><RowDefinition Height="Auto" /><RowDefinition Height="Auto" /><RowDefinition Height="Auto" /></Grid.RowDefinitions><ScrollViewer Grid.Row="1" Margin="5,5,5,5" HorizontalAlignment="Center" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" Style="{StaticResource MP_ScrollViewerStyle}" BorderBrush="{x:Null}"><toolKit:ListBoxDragDropTarget AllowDrop="True" Grid.Row="1" Grid.Column="1"><ListBox x:Name="GroupFunctionListBox" Width="200" Height="Auto" SelectionMode="Extended" ItemsSource="{Binding GroupFunctionList}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Foreground="White" Background="{StaticResource BGBrush_1}"><ListBox.ItemTemplate><DataTemplate><TextBlock Text="{Binding Label}" Style="{Binding IsInGroup, Converter={StaticResource StyleFunctionConverter}}" /></DataTemplate></ListBox.ItemTemplate></ListBox></toolKit:ListBoxDragDropTarget></ScrollViewer> //These are the two buttons (=> and<=, the content is &lg and >) <Button Content="<=" x:Name="AddButton" Grid.Row="1" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="10,0,10,40" VerticalAlignment="Center" Width="75" Click="AddButton_Click" Style="{StaticResource ButtonStyle_Blue}" /><Button Content="=>" x:Name="RemoveButton" Grid.Row="1" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="10,80,10,0" VerticalAlignment="Center" Width="75" Click="RemoveButton_Click" Style="{StaticResource ButtonStyle_Blue}" /><ScrollViewer Grid.Row="1" Grid.Column="2" Margin="5,5,5,5" HorizontalAlignment="Center" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" Style="{StaticResource MP_ScrollViewerStyle}" BorderBrush="{x:Null}"><toolKit:ListBoxDragDropTarget AllowDrop="True" Grid.Row="1" Grid.Column="2"><ListBox x:Name="OtherFunctionListBox" Width="200" HorizontalAlignment="Center" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" Height="Auto" SelectionMode="Extended" ItemsSource="{Binding OtherFunctionList}" Foreground="White" Background="{StaticResource BGBrush_1}"><ListBox.ItemTemplate><DataTemplate><TextBlock Text="{Binding Label}" Style="{Binding IsInGroup, Converter={StaticResource StyleFunctionConverter}}" /></DataTemplate></ListBox.ItemTemplate></ListBox></toolKit:ListBoxDragDropTarget></ScrollViewer></Grid>
My converter class is : (StyleFunctionConverter)
public class StyleFunctionConverter : IValueConverter { public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture) { if (value == null) return null; bool isInGroup = (bool)value; if (isInGroup) return Application.Current.Resources["ListBoxTextNormalItem"]; else return Application.Current.Resources["ListBoxTextChangeItem"]; } public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture) { return null; } }
button click events are :
private void AddButton_Click(object sender, RoutedEventArgs e) { ((GroupViewModel)this.DataContext).AddFunctionGroup((ObservableCollection<object>)OtherFunctionListBox.SelectedItems); }
View Model class is:
public ObservableCollection<Function> GroupFunctionList { get { return groupFunctionList; } set { groupFunctionList = value; NotifyPropertyChanged("GroupFunctionList"); } } private ObservableCollection<Function> otherFunctionList; /// <summary> /// groupList /// </summary> public ObservableCollection<Function> OtherFunctionList { get { return otherFunctionList; } set { otherFunctionList = value; NotifyPropertyChanged("OtherFunctionList"); } } public class StyleFunctionConverter : IValueConverter { public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture) { if (value == null) return null; bool isInGroup = (bool)value; if (isInGroup) return Application.Current.Resources["ListBoxTextNormalItem"]; else return Application.Current.Resources["ListBoxTextChangeItem"]; } public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture) { return null; } } button click events are : private void AddButton_Click(object sender, RoutedEventArgs e) { ((GroupViewModel)this.DataContext).AddFunctionGroup((ObservableCollection<object>)OtherFunctionListBox.SelectedItems); } And ViewModel Class is: public void AddFunctionGroup(ObservableCollection<object> selectedOtherFunction) { if (selectedOtherFunction != null) { List<Function> funcList = new List<Function>(); foreach (object func in selectedOtherFunction) funcList.Add((Function)func); foreach (Function func in funcList.OrderBy(c => c.Label)) { if (SelectedGroup != null && !SelectedGroup.GroupFunctionList.Contains(func)) func.IsInGroup = false; else func.IsInGroup = true; GroupFunctionList.Add(func); OtherFunctionList.Remove(func); } reorderGroup(); } } public void reorderGroup() { List<Function> TMPGroupFunctionList = new List<Function>(); foreach (Function gfl in GroupFunctionList.OrderBy(c => c.Label)) { TMPGroupFunctionList.Add(gfl); } GroupFunctionList.Clear(); foreach (Function gfl in TMPGroupFunctionList.OrderBy(c => c.Label)) { GroupFunctionList.Add(gfl); } List<Function> TMPOtherFunctionList = new List<Function>(); foreach (Function ofl in OtherFunctionList.OrderBy(c => c.Label)) { TMPOtherFunctionList.Add(ofl); } OtherFunctionList.Clear(); foreach (Function ofl in TMPOtherFunctionList.OrderBy(c => c.Label)) { OtherFunctionList.Add(ofl); } }