首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么在我的ListBox拖放中文本颜色不变?

为什么在我的ListBox拖放中文本颜色不变?
EN

Stack Overflow用户
提问于 2014-09-24 18:12:06
回答 1查看 196关注 0票数 0

我正在尝试从一个列表拖放到另一个列表。我尝试了下面的代码。它可以正确地拖放,但不会更改拖放到另一个列表中的文本的颜色。(我希望放置在旁边的文本的颜色发生变化,这样就可以配置知道哪个文本被放置了(假设是蓝色))。

我的MainPage.xaml是这样的:

代码语言:javascript
复制
<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>

MainPage.Xaml.cs类是:

代码语言:javascript
复制
public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            customerListBoxMain.ItemsSource = PersonDataProvider.GetData();
        }
    }

PersonDataProvider.cs类是:

代码语言:javascript
复制
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" }
                        };
        }
    }

和Person.cs (模型为)

代码语言:javascript
复制
public class Person
    {
        public string Name { get; set; }
    }

和Styles.xaml (资源字典)

代码语言:javascript
复制
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="ListBoxTextNormalItem" TargetType="TextBlock">
        <Setter Property="Foreground">
            <Setter.Value>
                <SolidColorBrush Color='Blue' />
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

App.xaml是:

代码语言:javascript
复制
<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;
            }
        }

如何将拖放文本的颜色设置为<SolidColorBrush Color='Blue' />

EN

回答 1

Stack Overflow用户

发布于 2014-09-24 19:17:54

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

如下所示:

代码语言:javascript
复制
    <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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26014265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档