1 Replies - 6028 Views - Last Post: 18 November 2013 - 02:34 AM Rate Topic: -----

#1 Eruthien  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 01-October 13

WPF: How to use variables declared in xaml.cs in the XAML file?

Posted 12 November 2013 - 01:57 PM

How do I use variables I declared in my xaml.cs file in the xaml file? Is it possible? On my application there is a open button which will allow you to choose an image in the file dialog and that image will become the background. In the xaml file I need a image source to set the background which is in the c# file so how do I get it into the xaml file?

Code:


Xaml.cs
namespace Test
{
    /// <summary>
    /// Interaction logic for Mainwindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Image_MouseEnter(object sender, MouseEventArgs e)
        {
            Image img = (Image)sender;

            DoubleAnimation fadeanim = new DoubleAnimation(1, TimeSpan.FromSeconds(0.25));

            img.BeginAnimation(Image.OpacityProperty, fadeanim);
        }

        private void Image_MouseLeave(object sender, MouseEventArgs e)
        {
            Image img = (Image)sender;

            DoubleAnimation fadeanim = new DoubleAnimation(0.4, TimeSpan.FromSeconds(0.25));

            img.BeginAnimation(Image.OpacityProperty, fadeanim);
        }

        private void Image_MouseDown(object sender, MouseButtonEventArgs e)
        {
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openfile = new Microsoft.Win32.OpenFileDialog();
            Nullable<bool> result = openfile.ShowDialog();

            
            if (result == true)
            {
                string backgroundpath = openfile.FileName;
            }
            
        }

       


    }
}





.xaml
<Window x:Class="brightness_text.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">
    <window.Background> 
        <ImageBrush ImageSource= openfile.Filename /> // What do I do here?
    </window.Background>
        <Grid Background="#FF302C2C">
        <Image HorizontalAlignment="Left" Height="29" Margin="214,280,0,0" VerticalAlignment="Top" Width="75" Source="C:\Users\eruth_000\Documents\Visual Studio 2012\Projects\brightness_text\brightness_text\bin\open_btn.png" MouseEnter="Image_MouseEnter" MouseLeave="Image_MouseLeave" Opacity="0.4" MouseLeftButtonDown="Image_MouseLeftButtonDown"/>

    </Grid>
</Window>


This post has been edited by Atli: 18 November 2013 - 02:46 AM
Reason for edit:: Title changed. Please use descriptive titles. It should tell us what you are asking, not just that you are asking something.


Is This A Good Question/Topic? 0
  • +

Replies To: WPF: How to use variables declared in xaml.cs in the XAML file?

#2 MrShoes  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 331
  • View blog
  • Posts: 512
  • Joined: 13-June 12

Re: WPF: How to use variables declared in xaml.cs in the XAML file?

Posted 18 November 2013 - 02:34 AM

If you're using the .xaml.cs file, then you usually manipulate the window from that. You do not access the code-behind from XAML unless you add it as a resource. To do that, in your .xaml.cs file you'll need an Image (I'll name this backgroundImage), then in the constructor, use Resources.Add("MyBackground", backgroundImage); once the Image has been instantiated. In your XAML, you would set then access it by the Resource name, like this: Background="{StaticResource MyBackground}". In you code, when the image hass been loaded into backgroundImage, then the UI should update.

You might be better declaring the resource in your XAML. In your XAML:
<Window x:Class="brightness_text.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" Background="{DynamicResource MyBackground}">
<window.Resources>
   <ImageBrush x:Key="MyBackground"/>
</window.Resources>
        <Grid Background="#FF302C2C">
        <Image HorizontalAlignment="Left" Height="29" Margin="214,280,0,0" VerticalAlignment="Top" Width="75" Source="C:\Users\eruth_000\Documents\Visual Studio 2012\Projects\brightness_text\brightness_text\bin\open_btn.png" MouseEnter="Image_MouseEnter" MouseLeave="Image_MouseLeave" Opacity="0.4" MouseLeftButtonDown="Image_MouseLeftButtonDown"/>

    </Grid>
</Window>



Then, once your user has selected an image and you've loaded it, you can do this: Resources["MyBackground"] = myImage;

The third, and best way, is to have a ViewModel class that implements INotifyPropertyChanged for the window as the DataSource, and bind the Window's Background to the Image property. <Window DataSource="{DynamicResource MyViewModel}" Background="{Binding MyBackground}"> <window.Resources> <my:WindowViewModel x:Key="MyViewModel"/> </window.Resources> In this way, you'll implement the preferred MVVM pattern, and separate the concerns of your UI from any background tasks easily (like loading the image etc.).

This post has been edited by MrShoes: 18 November 2013 - 02:35 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1