Click here to Skip to main content
15,888,733 members
Home / Discussions / WPF
   

WPF

 
QuestionExport WPF ListView data to excel Pin
Pankaj Chamria16-Mar-09 3:52
Pankaj Chamria16-Mar-09 3:52 
AnswerRe: Export WPF ListView data to excel Pin
Eslam Afifi16-Mar-09 8:02
Eslam Afifi16-Mar-09 8:02 
GeneralRe: Export WPF ListView data to excel Pin
zameb30-Aug-10 23:00
zameb30-Aug-10 23:00 
Questioncreate a button dynamically Pin
anishkannan16-Mar-09 1:59
anishkannan16-Mar-09 1:59 
QuestionRe: create a button dynamically Pin
Mark Salsbery16-Mar-09 5:42
Mark Salsbery16-Mar-09 5:42 
AnswerRe: create a button dynamically Pin
anishkannan16-Mar-09 6:54
anishkannan16-Mar-09 6:54 
AnswerRe: create a button dynamically Pin
Eslam Afifi16-Mar-09 7:49
Eslam Afifi16-Mar-09 7:49 
GeneralRe: create a button dynamically Pin
Eslam Afifi16-Mar-09 12:25
Eslam Afifi16-Mar-09 12:25 
I discourage the use of this method unless you really "really" have to put your class in a single xaml file. I can't even think of a scenario where you'd have to use this method.

I've just found out that you can place both the markup and the code in the xaml file.
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="600" Width="300">
    <Grid>
        <Grid.Resources>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Background" Value="Azure" />
            </Style>
        </Grid.Resources>

        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Grid.Children>
            <StackPanel x:Name="stack" />
            <Button HorizontalAlignment="Center" Grid.Row="1" Content="Add button" Click="Button_Click" />
        </Grid.Children>
    </Grid>


    <x:Code>
        <![CDATA[
            public Window1()
            {
                InitializeComponent();
            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                var button = new Button() { Content = "Newly added button" };
                var rnd = new Random(DateTime.Now.ToBinary().GetHashCode());
                
                switch (rnd.Next(3))
                {
                    case 0:
                        button.Background = Brushes.BlueViolet;
                        break;
                    case 1:
                        var style = new Style();
                        style.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.DarkBlue));
                        button.Style = style;
                        break;
                }

                stack.Children.Add(button);
            }
        ]]>
    </x:Code>
</Window>


You don't have to place the code in a CDATA section. You even can do this.
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="600" Width="300">
    <Grid>
        <Grid.Resources>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Background" Value="Azure" />
            </Style>
        </Grid.Resources>

        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Grid.Children>
            <StackPanel x:Name="stack" />

            <x:Code>
                public Window1()
            {
                InitializeComponent();
            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                var button = new Button() { Content = "Newly added button" };
                var rnd = new Random(DateTime.Now.ToBinary().GetHashCode());
                
                switch (rnd.Next(3))
            </x:Code>

            <Button HorizontalAlignment="Center" Grid.Row="1" Content="Add button" Click="Button_Click" />
        </Grid.Children>
    </Grid>

    <x:Code>
                {
                    case 0:
                        button.Background = Brushes.BlueViolet;
                        break;
                    case 1:
                        var style = new Style();
                        style.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.DarkBlue));
                        button.Style = style;
                        break;
                }

                stack.Children.Add(button);
            }
    </x:Code>
</Window>


Eslam Afifi

GeneralRe: create a button dynamically Pin
anishkannan16-Mar-09 18:57
anishkannan16-Mar-09 18:57 
AnswerRe: create a button dynamically Pin
Eslam Afifi16-Mar-09 23:07
Eslam Afifi16-Mar-09 23:07 
QuestionNew article to help you answer your questions on WPF Pin
TechiGIRL15-Mar-09 21:20
TechiGIRL15-Mar-09 21:20 
AnswerRe: New article to help you answer your questions on WPF Pin
TechiGIRL25-Mar-09 22:33
TechiGIRL25-Mar-09 22:33 
QuestionWPf trreview Insertion with binding Pin
thangavel198715-Mar-09 21:03
thangavel198715-Mar-09 21:03 
AnswerRe: WPf trreview Insertion with binding Pin
ABitSmart15-Mar-09 21:42
ABitSmart15-Mar-09 21:42 
AnswerRe: WPf trreview Insertion with binding Pin
Eslam Afifi16-Mar-09 1:51
Eslam Afifi16-Mar-09 1:51 
QuestionPopup window in silverlight Pin
sumit703415-Mar-09 19:31
sumit703415-Mar-09 19:31 
AnswerRe: Popup window in silverlight Pin
Pete O'Hanlon15-Mar-09 23:02
mvePete O'Hanlon15-Mar-09 23:02 
GeneralRe: Popup window in silverlight Pin
sumit703415-Mar-09 23:45
sumit703415-Mar-09 23:45 
GeneralRe: Popup window in silverlight Pin
Mark Salsbery16-Mar-09 6:25
Mark Salsbery16-Mar-09 6:25 
AnswerRe: Popup window in silverlight Pin
Braulio Dez1-Apr-09 6:02
Braulio Dez1-Apr-09 6:02 
QuestionWPF Weird behavior or me? [modified] Pin
Mike Hankey15-Mar-09 13:07
mveMike Hankey15-Mar-09 13:07 
AnswerRe: WPF Weird behavior or me? Pin
Mark Salsbery15-Mar-09 18:45
Mark Salsbery15-Mar-09 18:45 
GeneralRe: WPF Weird behavior or me? Pin
Mike Hankey16-Mar-09 1:48
mveMike Hankey16-Mar-09 1:48 
AnswerRe: WPF Weird behavior or me? [modified] Pin
Pete O'Hanlon15-Mar-09 22:44
mvePete O'Hanlon15-Mar-09 22:44 
GeneralRe: WPF Weird behavior or me? Pin
Mike Hankey16-Mar-09 1:47
mveMike Hankey16-Mar-09 1:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.