English 中文(简体)
乙烯: Can t 改革回合 其中特别纽州?
原标题:SyleProblem: Can t Change BackGround Of Particular button?
  1. I have one ==> UserControl.
  2. Inside that UserControl ==> One ItemsControl.

    Now ItemsControl generating Button’s As per ItemsSource given to it. I have given some style for those buttons .

    ==>Button s inside Pages.xaml.

    =>and Format within DataPagerResourceDictionary.xaml

I have using UserControl inside my Maindwindow.xaml. but i can t change Background of Particular Button based on Content Of Button.

www.un.org/Depts/DGACM/index_spanish.htm http://pritesharyan.weebly.com/downloadcode.html” rel=“nofollow”>。

如果我不向纽托州提供风格,则以下的法典将予以罚款。

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

I have Provided style to button inside following Code snippet [Have look insdie Pages.xaml].

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

Or Have 看一看。

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 [USER CONTROL]

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

www.un.org/Depts/DGACM/index_spanish.htm STYLE OF BUTTON: 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>

页: 1

最佳回答

You are overwritting the Button s Template in your Style, so the Background color never gets used

缺省模板就是这样:

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

并且你对模板表示过激。

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

你们需要将边界背景颜色与<条码>-TemplateBled 背景>挂钩,以便使用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>   
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签