Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我的列表框中的文本颜色不改变?_C#_.net_Wpf_Drag And Drop_Listbox - Fatal编程技术网

C# 为什么我的列表框中的文本颜色不改变?

C# 为什么我的列表框中的文本颜色不改变?,c#,.net,wpf,drag-and-drop,listbox,C#,.net,Wpf,Drag And Drop,Listbox,我正在尝试从一个列表拖放到另一个列表。我尝试了下面的代码。它可以正确地拖放,但不会更改拖放到其他列表的文本的颜色。(我希望在“下一步”中删除的文本的颜色发生变化,以便可以知道删除了哪个文本(假设为蓝色)) 我有MainPage.xaml,如下所示: <UserControl x:Class="Silverlight4.DragDropListBox.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/prese

我正在尝试从一个列表拖放到另一个列表。我尝试了下面的代码。它可以正确地拖放,但不会更改拖放到其他列表的文本的颜色。(我希望在“下一步”中删除的文本的颜色发生变化,以便可以知道删除了哪个文本(假设为蓝色))

我有MainPage.xaml,如下所示:

<UserControl x:Class="Silverlight4.DragDropListBox.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:toolKit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
             xmlns:local="clr-namespace:Silverlight4.DragDropListBox">

    <UserControl.Resources>
        <local:StyleFunctionConverter x:Key="StyleFunctionConverter"/>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBlock Text="Drag &amp; Drop ListBox Demo" FontSize="20" FontWeight="Bold" Foreground="Red" Margin="10"  Grid.Row="0"/>
        <StackPanel Orientation="Horizontal" Margin="10" Grid.Row="1">
            <toolKit:ListBoxDragDropTarget AllowDrop="True">
                <ListBox x:Name="customerListBoxMain" Height="200" Width="200" 
                            DisplayMemberPath="Name"  Style="{Binding IsInGroup,  Converter={StaticResource StyleFunctionConverter}}">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Vertical"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                </ListBox>
            </toolKit:ListBoxDragDropTarget>
            <TextBlock Width="20" />
            <toolKit:ListBoxDragDropTarget AllowDrop="True">
                <ListBox Height="200" Width="200" DisplayMemberPath="Name" Style="{Binding IsInGroup, Converter={StaticResource StyleFunctionConverter}}">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Vertical"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                </ListBox>
            </toolKit:ListBoxDragDropTarget>
        </StackPanel>
    </Grid>
</UserControl>
    <ListBox>
        <ListBox.Resources>
            <Style TargetType="ListBoxItem">
                <Setter Property="Foreground" Value="Green" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=DataContext.IsInGroup}" Value="True">
                        <Setter Property="Foreground" Value="Blue"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListBox.Resources>
    </ListBox>
类PersonDataProvider.cs是:

public class PersonDataProvider
    {
        public static ObservableCollection<Person> GetData()
        {
            return new ObservableCollection<Person>
                        {
                            new Person { Name = "Akash Sharma" },
                            new Person { Name = "Vinay Sen" },
                            new Person { Name = "Lalit Narayan" },
                            new Person { Name = "Madhumita Chatterjee" },
                            new Person { Name = "Priyanka Patil" },
                            new Person { Name = "Kumar Sanu" },
                            new Person { Name = "Victor Kapoor" },
                            new Person { Name = "Shymal Sen" },
                            new Person { Name = "Alan D'Souza" },
                            new Person { Name = "Kamal Saha" },
                            new Person { Name = "Alex Chan" },
                            new Person { Name = "Rohit Sharma" },
                            new Person { Name = "Dipti Sen" },
                            new Person { Name = "Dinesh Sharma" },
                            new Person { Name = "Kamal Kapoor" },
                            new Person { Name = "Raj Kapoor" },
                            new Person { Name = "Deepa Karmakar" },
                            new Person { Name = "Sarmishtha Chakrobarty" },
                            new Person { Name = "Pranab Kumar Debnath" },
                            new Person { Name = "Hiral Grover" },
                            new Person { Name = "Munmun Patel" },
                            new Person { Name = "Santosh Kumar Sen" },
                            new Person { Name = "Sandeep Debnath" }
                        };
        }
    }
和Styles.xaml(资源字典)


App.xaml是:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             x:Class="Silverlight4.DragDropListBox.App">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Styles.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>


And StyleFunctionConverter.cs is:


    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;
            }
        }

而StyleFunctionConverter.cs是:
公共类StyleFunctionConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
如果(值==null)
返回null;
bool isInGroup=(bool)值;
如果(ISingGroup)
返回Application.Current.Resources[“ListBoxTextNormalItem”];
其他的
返回Application.Current.Resources[“ListBoxTextChangeItem”];
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
返回null;
}
}

如何将拖放文本的颜色设置为

我宁愿在XAML中使用DataTriggers,而不是绑定样式。通过这种方式,您可以根据可以使用绑定访问的数据更改样式

大概是这样的:

<UserControl x:Class="Silverlight4.DragDropListBox.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:toolKit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
             xmlns:local="clr-namespace:Silverlight4.DragDropListBox">

    <UserControl.Resources>
        <local:StyleFunctionConverter x:Key="StyleFunctionConverter"/>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBlock Text="Drag &amp; Drop ListBox Demo" FontSize="20" FontWeight="Bold" Foreground="Red" Margin="10"  Grid.Row="0"/>
        <StackPanel Orientation="Horizontal" Margin="10" Grid.Row="1">
            <toolKit:ListBoxDragDropTarget AllowDrop="True">
                <ListBox x:Name="customerListBoxMain" Height="200" Width="200" 
                            DisplayMemberPath="Name"  Style="{Binding IsInGroup,  Converter={StaticResource StyleFunctionConverter}}">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Vertical"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                </ListBox>
            </toolKit:ListBoxDragDropTarget>
            <TextBlock Width="20" />
            <toolKit:ListBoxDragDropTarget AllowDrop="True">
                <ListBox Height="200" Width="200" DisplayMemberPath="Name" Style="{Binding IsInGroup, Converter={StaticResource StyleFunctionConverter}}">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Vertical"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                </ListBox>
            </toolKit:ListBoxDragDropTarget>
        </StackPanel>
    </Grid>
</UserControl>
    <ListBox>
        <ListBox.Resources>
            <Style TargetType="ListBoxItem">
                <Setter Property="Foreground" Value="Green" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=DataContext.IsInGroup}" Value="True">
                        <Setter Property="Foreground" Value="Blue"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListBox.Resources>
    </ListBox>

您的风格目标<代码>。。。然后在代码中尝试将
列表框
样式设置为该样式。也许这就是原因?我做了改变,但仍然没有工作。您认为这是问题所在吗?SolidColorBrush Color='Blue'>。我将SolidColorBrush改为蓝色,因为我无法在那里获得“背景”和“FontColor”属性。
    <ListBox>
        <ListBox.Resources>
            <Style TargetType="ListBoxItem">
                <Setter Property="Foreground" Value="Green" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=DataContext.IsInGroup}" Value="True">
                        <Setter Property="Foreground" Value="Blue"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListBox.Resources>
    </ListBox>