Skip to main content
 首页 » 编程设计

c#之语法问题 : Can't Change BackGround Of Particular button

2024年02月23日13rubylouvre

  1. 我有一个 ==> UserControl。
  2. 在那个 UserControl 里面 ==> 一个 ItemsControl。

    现在 ItemsControl 生成 Button 的 根据 ItemsSource 给它。 我为这些按钮提供了一些样式。

    ==>按钮在 Pages.xaml 中。

    ==>和 DataPagerResourceDictionary.xaml 中的样式。

我在 Mainwindow.xaml 中使用了 UserControl。 但我无法根据按钮内容更改特定按钮的背景。

您可以从 here 下载完整代码.

如果我不为按钮提供样式,下面的代码可以正常工作。

for (int i = 0; i < displayPages.pageControl.Items.Count; i++) 
        { 
            var container = displayPages.pageControl.ItemContainerGenerator.ContainerFromIndex(i) as ContentPresenter; 
            var button = container.ContentTemplate.FindName("pageNumberButton", container) as Button; 
            if (button.Content == "  3  ") 
            { 
                button.Background = Brushes.Red; 
            } 
        }  

我已经提供了样式到下面的代码片段中的按钮 [看看独立的 Pages.xaml]。

<ItemsControl.ItemTemplate> 
                <DataTemplate> 
                    <Button x:Name="pageNumberButton"  Style="{StaticResource TransparentButton}"   Content="{Binding Path=Page_Number}"></Button> 
                </DataTemplate> 
            </ItemsControl.ItemTemplate> 

或者看看下面的代码:

MainWindow.xaml

<Window x:Class="StylingProblem.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" Height="350" Width="525" 
        xmlns:Local="clr-namespace:StylingProblem"> 
    <Grid> 
        <Local:Pages x:Name="displayPages"></Local:Pages> 
        <Button Content="Change Color Button" Height="23" HorizontalAlignment="Left" Margin="149,164,0,0" Name="button1" VerticalAlignment="Top" Width="133" Click="button1_Click" /> 
    </Grid> 
</Window> 

MainWindow.xaml.cs

public partial class MainWindow : Window 
    { 
        ObservableCollection<PageNumber> pageCollection = new ObservableCollection<PageNumber>(); 
 
        public MainWindow() 
        { 
            InitializeComponent(); 
            pageCollection.Add(new PageNumber("  1  ")); 
            pageCollection.Add(new PageNumber("  2  ")); 
            pageCollection.Add(new PageNumber("  3  ")); 
            pageCollection.Add(new PageNumber("  4  ")); 
            pageCollection.Add(new PageNumber("  5  ")); 
            pageCollection.Add(new PageNumber("  6  ")); 
 
            this.DataContext = this; 
        } 
 
        public ObservableCollection<PageNumber> PageCollection 
        { 
            get 
            { 
                return this.pageCollection; 
            } 
            set 
            { 
                this.pageCollection = value; 
            } 
        } 
 
        private void button1_Click(object sender, RoutedEventArgs e) 
        { 
            //Chage Color of Perticular button here 
            //Suppose say change the color of button with content == "  3  " 
 
            #region --  THIS CODE WORKS FINE IF I DON'T PROVIDE ANY STYLE TO BUTTON [see Pages.xaml]  -- 
            for (int i = 0; i < displayPages.pageControl.Items.Count; i++) 
            { 
                var container = displayPages.pageControl.ItemContainerGenerator.ContainerFromIndex(i) as ContentPresenter; 
                var button = container.ContentTemplate.FindName("pageNumberButton", container) as Button; 
                if (button.Content == "  3  ") 
                { 
                    button.Background = Brushes.Red; 
                } 
            }  
            #endregion 
 
 
        } 

Pages.xaml [用户控制]

<UserControl x:Class="StylingProblem.Pages" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
             mc:Ignorable="d"  
              > 
    <UserControl.Resources> 
        <ResourceDictionary Source="DataPagerResourceDictionary.xaml"/> 
    </UserControl.Resources> 
    <Grid> 
        <ItemsControl Name="pageControl" ItemsSource="{Binding Path=PageCollection}"> 
            <ItemsControl.Template> 
                <ControlTemplate TargetType="ItemsControl"> 
                    <Border > 
                        <StackPanel> 
                            <ItemsPresenter></ItemsPresenter> 
                        </StackPanel> 
                    </Border> 
                </ControlTemplate> 
            </ItemsControl.Template> 
            <ItemsControl.ItemsPanel x:Uid="pageItemTemplate"> 
                <ItemsPanelTemplate> 
                    <StackPanel Orientation="Horizontal"/> 
                </ItemsPanelTemplate> 
            </ItemsControl.ItemsPanel> 
            <ItemsControl.ItemTemplate> 
                <DataTemplate> 
                    <Button x:Name="pageNumberButton" Style="{StaticResource TransparentButton}"  Content="{Binding Path=Page_Number}"></Button> 
                </DataTemplate> 
            </ItemsControl.ItemTemplate> 
 
        </ItemsControl> 
    </Grid> 
</UserControl> 

按钮样式:DataPagerResourceDictionary.xaml

<ResourceDictionary 
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
   > 
    <Style TargetType="Button" x:Key="TransparentButton"> 
        <Setter Property="Template"> 
            <Setter.Value> 
                <ControlTemplate TargetType="Button"> 
                    <Border  CornerRadius="2,2,2,2"  HorizontalAlignment="Center" x:Name="borderTemplate" Background="Transparent"> 
                        <ContentPresenter/> 
                    </Border> 
                    <ControlTemplate.Triggers> 
                        <Trigger Property="IsMouseOver" Value="true"> 
                            <Setter TargetName="borderTemplate"  Property="Border.BorderBrush" Value="Gray" /> 
                            <Setter TargetName="borderTemplate"  Property="Border.BorderThickness" Value="1" /> 
                        </Trigger> 
                        <Trigger Property="IsPressed" Value="true"> 
                            <Setter TargetName="borderTemplate"  Property="Border.BorderBrush" Value="Lime" /> 
                        </Trigger> 
                        <Trigger Property="IsFocused" Value="true"> 
                            <Setter TargetName="borderTemplate"  Property="Border.Background" Value="#FD7" /> 
                        </Trigger> 
                        <Trigger Property="IsEnabled" Value="false"> 
                            <Setter TargetName="borderTemplate"  Property="Border.Background" Value="LightGray"></Setter> 
                        </Trigger> 
                    </ControlTemplate.Triggers> 
                </ControlTemplate> 
            </Setter.Value> 
        </Setter> 
    </Style> 
</ResourceDictionary> 

谢谢......................................

请您参考如下方法:

您在样式中覆盖了按钮的模板,因此永远不会使用背景颜色

默认按钮模板如下所示:

<Button Background="SomeColor"> 
    <Button.Content> 
</Button> 

你正在覆盖模板说

<Border> 
    <Button.Content> 
</Border> 

您需要将 Border 的背景色绑定(bind)到 {TemplateBinding Background},以便它使用 Button 的背景色。

我还建议使用 DataTrigger 而不是代码隐藏来更改按钮的背景颜色。

<Style TargetType="{x:Type Button}"> 
    <Setter Property="Background" Value="Transparent" /> 
    <Style.Triggers> 
        <DataTrigger Binding="{Binding }" Value="3"> 
            <Setter Property="Background" Value="Red" /> 
        </DataTrigger> 
    </Style.Triggers> 
</Style>