C# SyleProblem:Can';不改变特定按钮的背景? 我有一个==>UserControl

C# SyleProblem:Can';不改变特定按钮的背景? 我有一个==>UserControl,c#,.net,wpf,xaml,styles,C#,.net,Wpf,Xaml,Styles,在该UserControl==>OneItemsControl 现在,根据提供给它的ItemsSource生成按钮的。 我为那些按钮设计了一些款式 ==>按钮的内部页面.xaml ==>和样式内部datapagersourcedictionary.xaml 我在Maindwindow.xaml中使用了UserControl。 但是我不能基于按钮的内容改变特定按钮的背景 您可以从下载完整的代码 如果我不为按钮提供样式,下面的代码可以正常工作 for (int i = 0; i < displ

在该UserControl==>One
ItemsControl

现在,根据提供给它的
ItemsSource
生成
按钮的
。
我为那些按钮设计了一些款式

==>按钮的内部
页面.xaml

==>和样式内部
datapagersourcedictionary.xaml

我在
Maindwindow.xaml
中使用了
UserControl
。 但是我不能基于按钮的内容改变特定按钮的背景

您可以从下载完整的代码

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

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;
            }
        } 
for(int i=0;i
我为下面的代码片段中的按钮提供了样式 [查看页面.xaml]

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

或查看以下代码:

main window.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>
<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>

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


        }
公共部分类主窗口:窗口
{
ObservableCollection pageCollection=新的ObservableCollection();
公共主窗口()
{
初始化组件();
pageCollection.Add(新的页码(“1”));
pageCollection.Add(新的页码(“2”));
pageCollection.Add(新页码(“3”));
pageCollection.Add(新页码(“4”);
pageCollection.Add(新的页码(“5”));
pageCollection.Add(新页码(“6”));
this.DataContext=this;
}
公共可观测集合页面集合
{
收到
{
返回此.pageCollection;
}
设置
{
this.pageCollection=值;
}
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
//在这里更改孔按钮的颜色
//假设更改内容为“3”的按钮的颜色
#region——如果我不为按钮提供任何样式,这段代码就可以正常工作[参见Pages.xaml]--
对于(int i=0;i
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

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

您正在以自己的样式覆盖按钮的模板,因此永远不会使用背景色

默认按钮模板如下所示:

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

您将样式分配给按钮的位置?您的样式有一个键(
TransparentButton
),所以它必须手动分配。@Rachel,请看我有更新问题。…syle在
DataPagerResourceDictionary.xaml中,我在
页面中使用它。xaml
。感谢您考虑我的问题。@All,我更新了
页面。xaml
@All,我更新了我的源代码。因为我忘了应用
样式=”{StaticResource TransparentButton}“
页面内的
按钮
。xaml
。您在哪里为按钮指定样式?您的样式有一个键(
TransparentButton
),所以它必须手动分配。@Rachel,请看我有更新问题。…syle在
DataPagerResourceDictionary.xaml中,我在
页面中使用它。xaml
。感谢您考虑我的问题。@All,我更新了
页面。xaml
@All,我更新了我的源代码。因为我忘了应用
样式=”{StaticResource TransparentButton}“
页面内的
按钮