如何更改文本块背景?

时间:2015-01-06 04:15:55

标签: c# wpf xaml textblock

这是我的xaml structure

<stackpanel>
  <textblock Text="A"></textblock>
  <textblock Text="B"></textblock>
</stackpanel>

Stackpanel,它可以循环生成更多相同的结构。

单击时如何更改texblock的背景颜色?

更新I just want change on xaml file not using C#.

默认我有黄色,但是当我点击一个文本块时,它的背景将变为蓝色。

更新2 :如果点击A,A将变为蓝色,直到我点击另一个。

详情:

  1. 点击A (A is yellow),A将变为蓝色==&gt; A是蓝色

  2. 我点击B (B is yellow and A is blue),B将变为蓝色,A将变为黄色。

  3. 更新3 :这个怎么样?

    <stackpanel>
          <textblock Text="A"></textblock>
    </stackpanel>
    
    <stackpanel>
          <textblock Text="A"></textblock>
    </stackpanel>
    <stackpanel>
          <textblock Text="A"></textblock>
    </stackpanel>
    

    问题与上述相同

    谢谢!

5 个答案:

答案 0 :(得分:3)

以编程方式:

textBlock1.Background = new SolidColorBrush(Colors.Yellow);

在XAML中:

<TextBlock Name="textBlock1">
    <TextBlock.Background>
        <SolidColorBrush Color="Yellow" />
    </TextBlock.Background>
</TextBlock>

要在单击textBlock时更改背景颜色,应使用带触发器的自定义样式。如果您要搜索或询问使用样式和触发器,这并不难。你可以定义在XAML设计模式中不应该使用C#。

答案 1 :(得分:2)

使用 EventTrigger 颜色动画,您可以在 MouseDown MouseLeave 上更改TextBlock背景颜色的颜色


xaml代码

 <StackPanel>
    <StackPanel.Resources>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <EventTrigger RoutedEvent="MouseDown">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetProperty="Background.Color" From="Yellow" To="Blue" Duration="0:0:0.1"></ColorAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="MouseLeave">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetProperty="Background.Color" From="Blue" To="Yellow" Duration="0:0:0.1"></ColorAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Resources>
    <TextBlock Text="A" Background="Yellow"></TextBlock>
    <TextBlock Text="B" Background="Yellow"></TextBlock>
</StackPanel>

<强>更新

使用带有属性IsReadOnly = True 的 TextBox而不是textblock。

    <StackPanel>
    <StackPanel.Resources>
        <Style TargetType="TextBox"> 
            <Setter Property="Background" Value="Yellow"></Setter>
            <Setter Property="BorderThickness" Value="0"></Setter> 
            <Setter Property="IsReadOnly" Value="True"></Setter>
            <Style.Triggers>
                <EventTrigger RoutedEvent="TextBox.GotFocus">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetProperty="Background.Color" From="Yellow" To="Blue" Duration="0:0:0.1"></ColorAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="TextBox.LostFocus">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetProperty="Background.Color" From="Blue" To="Yellow" Duration="0:0:0.1"></ColorAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Resources>
    <TextBox Text="A"></TextBox>
    <TextBox Text="A"></TextBox>
</StackPanel>

答案 2 :(得分:0)

  <TextBlock Text="Hello, styled world!" FontSize="28" HorizontalAlignment="Center" VerticalAlignment="Center">
                <TextBlock.Style>
                    <Style TargetType="TextBlock">
                        <Setter Property="Background" Value="Yellow"></Setter>
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="Blue" />
                                <Setter Property="TextDecorations" Value="Underline" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>

答案 3 :(得分:0)

这将是最有可能解决您问题的方法。正如我所指出的那样,由于您的要求的性质,您希望一个控件依赖于其他控件的各种控制,因此不可能只使用xaml解决方案。

当然,你可以摆弄其余部分。

public class RadioTextblock : TextBlock
{
    static RadioTextblock()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(RadioTextblock), new FrameworkPropertyMetadata(typeof(RadioTextblock)));
    }

    public static readonly DependencyProperty GroupProperty = DependencyProperty.Register(
        "Group", typeof (string), typeof (RadioTextblock), new PropertyMetadata(string.Empty));

    public string Group
    {
        get { return (string) GetValue(GroupProperty); }
        set { SetValue(GroupProperty, value); }
    }

    public static readonly DependencyProperty ActiveColorProperty = DependencyProperty.Register(
        "ActiveColor", typeof (Brush), typeof (RadioTextblock), new PropertyMetadata(default(Brush)));

    public Brush ActiveColor
    {
        get { return (Brush) GetValue(ActiveColorProperty); }
        set { SetValue(ActiveColorProperty, value); }
    }

    public static readonly DependencyProperty RestorationColorProperty = DependencyProperty.Register(
        "RestorationColor", typeof (Brush), typeof (RadioTextblock), new PropertyMetadata(default(Brush)));

    public Brush RestorationColor
    {
        get { return (Brush) GetValue(RestorationColorProperty); }
        set { SetValue(RestorationColorProperty, value); }
    }

    protected override void OnInitialized(EventArgs e)
    {
        base.OnInitialized(e);
        // there may be a better hook for this. but i'm not writing custom controls that often. anything after styles are applied should be good
        RestorationColor = Background;
    }

    protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
    {
        RestoreOtherBackgrounds(this);
        base.OnPreviewMouseDown(e);
    }

    private void RestoreOtherBackgrounds(RadioTextblock radioTextblock)
    {
        var topParent = GetTopMostParent(radioTextblock);
        var controlsWithGroup = GetChildrenRecursive<RadioTextblock>(topParent).ToLookup(d => (string)d.GetValue(GroupProperty));
        var similar = controlsWithGroup[radioTextblock.Group];
        foreach (var match in similar)
        {
            if (match == radioTextblock)
            {
                match.Background = ActiveColor;
            }
            else
            {
                match.Background = match.RestorationColor;
            }
        }
    }

    private DependencyObject GetTopMostParent(RadioTextblock radioTextblock)
    {
        DependencyObject current = radioTextblock;
        while (current != null)
        {
            var cParent = VisualTreeHelper.GetParent(current);
            if (cParent == null || cParent == current)
                break;

            current = cParent;
        }

        return current;
    }

    private IEnumerable<T> GetChildrenRecursive<T>(DependencyObject current) where T : DependencyObject
    {
        T casted;
        var childCount = VisualTreeHelper.GetChildrenCount(current);
        for (int i = 0; i < childCount; i++)
        {
            var currentChild = VisualTreeHelper.GetChild(current, i);
            casted = currentChild as T;
            if(casted != null)
                yield return casted;

            foreach (var subChild in GetChildrenRecursive<T>(currentChild))
            {
                if (subChild != null)
                    yield return casted;
            }
        }
    }
}

答案 4 :(得分:0)

更改您的XAML结构,如下所示

xmlns:local="clr-namespace:your_assembly_Name"
.....  
<StackPanel>
    <StackPanel.Resources>
        <local:BackgroundConverter x:Key="backgroundConverter"/>
        <Style TargetType="TextBlock">
            <EventSetter Event="MouseDown" Handler="TextBlockMouseDownEvent" />
            <Setter Property="Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource backgroundConverter}">
                        <Binding Path="Name" RelativeSource="{RelativeSource Self}"/>
                        <Binding Path="SelectedTextBlockName"/>
                    </MultiBinding>
                </Setter.Value>
            </Setter>
        </Style>
    </StackPanel.Resources>
    <TextBlock x:Name="text1" Text="Header"/>
    <TextBlock x:Name="text2" Text="Header"/>
    <TextBlock x:Name="text3" Text="Header"/>
    <TextBlock x:Name="text4" Text="Header"/>
</StackPanel>

在您的代码中使用以下IMultiValueConverter和添加TextBlockMouseDownEvent事件处理程序以及一个DependencyProperty

////DependencyProperty
public string SelectedTextBlockName
{
    get { return (string)GetValue(SelectedTextBlockNameProperty); }
    set { SetValue(SelectedTextBlockNameProperty, value); }
}

// Using a DependencyProperty as the backing store for SelectedTextBlock.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedTextBlockNameProperty =
    DependencyProperty.Register("SelectedTextBlockName", typeof(string), typeof(Class_Name), new PropertyMetadata(null));

.......

private void TextBlockMouseDownEvent(object sender, MouseButtonEventArgs e)
{
    TextBlock txtBlock = sender as TextBlock;
    if (txtBlock != null)
    {
        SelectedTextBlockName = txtBlock.Name;
    }
}

.......

public class BackgroundConverter : IMultiValueConverter
{
    /// <summary>
    /// Values[0] = Name of TextBlock
    /// values[1] = SelectedTextBlockName
    /// If matches then it is selected
    /// </summary>
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (values[0] != null && values[1] != null && values[0].ToString() == values[1].ToString())
            return new SolidColorBrush(Colors.Blue);
        return new SolidColorBrush(Colors.White);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

如果要设置默认的SelectedTextBlock,则只需将您想要的TextBlock名称设置为默认选择。 像这样:

SelectedTextBlockName = "text1";