我有两个ListBox,每个都有一个TextBlock(来自DataTemplate)。我有两个按钮将文本从一个列表框传输到另一个列表框,反之亦然。
我还允许用户拖放以将文本从一个ListBox移动到另一个ListBox,这在功能上可以正常工作。
但是对于我的按钮,我使用"Style="{Binding IsInGroup, Converter={StaticResource StyleFunctionConverter}}"
当所选按钮出现在按钮上的另一个列表上时,它会改变文本样式和颜色,但是当我尝试使用拖放来传输文本时,它只是简单地传输文本而不会改变文本颜色。
我的代码:
<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>
我的转换器类是:(StyleFunctionConverter)
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");
}
}
按钮点击事件是:
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);
}
}
价值Coverter就像这样(风格):
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;
}
}