Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I have created a custom control library called Base_Dialog, and in that library I created a control, BaseDlg, like this:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Base_Dialog
{
    public class BaseDlg : Window
    {
        static BaseDlg()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(BaseDlg), new FrameworkPropertyMetadata(typeof(BaseDlg)));
        }
    }
}


And in "Generic.xaml":
XML
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Base_Dialog"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
    <Style TargetType="{x:Type local:BaseDlg}" x:Name="local1">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:BaseDlg}">
                    <Grid Name="gidBaseDlg" >
                        <Grid.Background>
                            <ImageBrush  ImageSource="/Base%20Dialog;component/Image/Window.png"/>
                        </Grid.Background>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <ContentPresenter x:Name="windowContent"
                            Content="{TemplateBinding Property=ContentControl.Content}"
                            Margin="4" Grid.Row="0" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>


And I wrote the following code in the "AssemblyInfo.cs" file:
C#
[assembly: XmlnsDefinition("http://schemas.sheva.com/winfx/2006/xaml/presentation", "Base_Dialog")]
then open wpfapplication name((wpf))and add references Project ((Base_Dialog))


The XAML for the control:
XML
<local:BaseDlg x:Class="wpf.Box"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="http://schemas.sheva.com/winfx/2006/xaml/presentation"
        Title="Box" Height="300" Width="300">
    <Grid>
        <Button Content="enas" Height="30" Width="30" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    </Grid>
</local:BaseDlg>


I want change the background for BaseDlg at runtime then have and every window inheriting from this BaseDlg use the same background.

How can I change the background for my custom control at runtme?
Posted
Updated 20-May-11 18:21pm
v7
Comments
AspDotNetDev 21-May-11 0:20am    
FYI, I made your question look a little nicer. Make sure to use PRE tags and CODE tags correctly next time you ask a question. There is a preview area that displays while you write your question to help you with that.

The style you have return doesn't uses the Background property. I have made some changes to the style. This would work.

XML
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Base_Dialog"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
    <Style TargetType="{x:Type local:BaseDlg}" x:Name="local1">
        <setter property="Background">
            <setter.value>
                <ImageBrush  ImageSource="/Base%20Dialog;component/Image/Window.png"/>
            </setter.value>
        </setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:BaseDlg}">
                    <Grid Name="gidBaseDlg" Background="{TemplateBinding Background}" >                        
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <ContentPresenter x:Name="windowContent"
                            Content="{TemplateBinding Property=ContentControl.Content}"
                            Margin="4" Grid.Row="0" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>



Now you can use Background property itself to change the Background of the CustomControl at runtime.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-May-11 15:00pm    
Thanks for this post and explanation. My 5.
--SA
Here is how:

C#
MyDialog.Background = Brushes.Wheat;


Now, Background is naturally an instance property, so during run-time it will only effects one instance. You cannot do anything to a class, even a single class, not to a class hierarchy. As it works per instance, you will need to do it to every instance you need to retouch. I don't think this is a problem. Maintain a collection of all your dialogs or windows, and so on.

—SA
 
Share this answer
 
Comments
Noze 21-May-11 0:49am    
look when i write this
MyDialog.Background = Brushes.Wheat;
this don't execute
i write in custom controle
grid and insert background into grid
but when i remove this background from grid
and write this in event lode_BaseDlg
this.Background = Brushes.Wheat;

the background display black just ....

placse help me ..........
thanks
Sergey Alexandrovich Kryukov 22-May-11 1:22am    
Hm...
Venkatesh Mookkan 24-May-11 2:40am    
He has tampered the CustomControl style and hardcoded the Background inside the Style. So, Background properly will never affect the CustomControl. See my reply.
Sergey Alexandrovich Kryukov 24-May-11 14:59pm    
I see what you mean. I expected something like that.
Thank you very much for your note. I'll vote...
--SA
Hm...

Maybe you can add static property, like "CommonBackground", and fire event like "CommonBackroundChanged" in it's setter.

Then, add code in the constructor, that will subscribe on "CommonBackroundChanged" event, and change current instance background in it's handler.
 
Share this answer
 
Comments
Venkatesh Mookkan 24-May-11 2:40am    
He has tampered the CustomControl style and hardcoded the Background inside the Style. So, Background properly will never affect the CustomControl. See my reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900