How to bind a image in Label in WPF using pure code? - wpf

I am new to WPF, here I am making a jigsaw game but encountering a problem: I just don't know how to let a image show in a Label in pure code (in XAML, I can do it.), I have searched google a lot but seems that no releated articles regarding this topic. Anyone can help me on this? thx.
My Code Sample:
Label lbl = new Label();
lbl.Width = 120;
lbl.Height = 120;
lbl.Background = new SolidColorBrush(Colors.YellowGreen);
BitmapImage myImageSource = new BitmapImage();
myImageSource.BeginInit();
myImageSource.UriSource = new Uri("Images/test.png",UriKind.Relative);
myImageSource.EndInit();
lbl.Background.SetValue(ImageBrush.ImageSourceProperty, myImageSource);
myGridContainer.Children.Add(lbl);
well, this code sample can't work. why ? any comment or answer from you will be appreciated , thx.

You can use BindingOperations.SetBinding to data-bind a DependencyProperty to its source.
See also: How can I create a tiled image in code that is also data-bound?

Related

How do I set alignment on DataGridTextColumn cell text using code

I'm trying to add DataGridTextColumn programmatically, and set some properties to present the text in cells.
I don't want to use TemplateColumn as it requires double-tab to enter edit mode.
I can't find code that would set alignment of the text.
Anyone has any idea how to achieve this? Thanks in advance.
I tried so far:
Dim txt As New DataGridTextColumn()
Dim c As New DataGridCell
c.HorizontalAlignment = HorizontalAlignment.Center
txt.CellStyle = c.Style
and
txt.SetValue(TextBox.HorizontalAlignmentProperty, HorizontalAlignment.Center)
and few others that I didn't keep track of.
Thanks for your help and I found the solution only with your guidance.
Especially thanks to #AnjumSKhan
This worked for me:
Dim txt As New DataGridTextColumn()
Dim s As New Style
s.Setters.Add(New Setter(TextBox.TextAlignmentProperty, TextAlignment.Right))
txt.CellStyle = s
Create a style, and then assign it to txt.CellStyle.
C# code :
Style s = new Style();
s.Setters.Add(new Setter(DataGridCell.HorizontalAlignmentProperty, HorizontalAlignment.Center));
txt.CellStyle = s;
In your c# code you have to use the index of the column. Here are some examples to set the cell alignment:
dataGrid.Columns[0].ItemStyle.HorizontalAlign = HorizontalAlign.Center;
dataGrid.Columns[0].ItemStyle.VerticalAlign = VerticalAlign.Middle;

WPF: put an icon on a radWindow programatically

I want to put an icon on the top left of a radWindow programatically
my code is like this
RadWindow radWindow = new RadWindow();
radWindow.Header = "The header";
radWindow.Icon = new Image()
{
Source = new BitmapImage(new Uri("../ressources/enregistrer.png", UriKind.Relative))
};
radWindow.Show();
but the icon dont show up
does anyone have an idea ?
EDIT
This is the architecture on my project:
The file from where the above code is taken is circled in red
The ressource file is circled in green
<telerik:RadWindow.Icon>
<Image Source="pack://application:,,,/ressources/enregistrer.png" Height="18"/>
</telerik:RadWindow.Icon>
// this seems to work. You will need to provide a size.
I dont know why my solution above did not work but I found a workaround like this
RadWindow radWindow = new RadWindow();
radWindow.Header = "The header";
radWindow.Icon = new Image()
{
Source = new BitmapImage(new Uri("pack://application:,,,/ressources/enregistrer.png", UriKind.RelativeOrAbsolute))
}
radWindow.Show();

How to change the background of a grid to an image in WP7 silverlight?

I'm trying to set a background for a grid control in WP7 silverlight, I need to do that programatically, not in the desighn.
I tried something like:
ContentPanel.Background = new BitmapImage(new Uri("Images\Backgrounds\with box\13.jpg", UriKind.Relative));
But of course, I got an error, because on the right hand we should have the type SolidColorBrush.
is there a way to do that?
Thanks..
I would firstly recommend that you use a Canvas instead of a Grid. You can do this by deleting the Grid and inserting a Canvas through the Toolbox -> Drag and Drop.
Then, you can use the code as simple as:
ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = new BitmapImage(new Uri("url", UriKind.Relative));
CanvasName.Background = imageBrush;
That will change the background to whatever you want.
Hope that helps.

Binding Image Control to URI in WP7

I ham developing an application for WP7 and i am having a strange problem with Image control, it does not show the image it is binded to.
I have tryed it in so many ways but event this way (the most naive one) does not work:
In the xaml I have an Image control named img and this is what I have done in the code behind
public MainPage()
{
InitializeComponent();
img.Source = new BitmapImage(new Uri (#"http://img11.imageshack.us/img11/5365/photovnj.jpg", UriKind.Absolute));
}
</code>
It seems to simple not to work...
Please help!
This code works fine for me presuming you have img declared as an Image in your xaml.
If not, I'd suggest you have a connection problem from your app running in the emulator or device to the internet.
This is the code I wrote.
image1.Source = new BitmapImage(new Uri(#"http://img11.imageshack.us/img11/5365/photovnj.jpg", UriKind.Absolute));

How to set uri for local image in silverlight app?

In SL class library MyLib, I have a image say my.png. Then I want to use it in code behind I tried following way:
StreamResourceInfo resourceStream = Application.GetResourceStream(new Uri("/MyLib;component/my.png", UriKind.Relative));
BitmapImage image = new BitmapImage();
image.SetSource(resourceStream.Stream);
this.MyIcon.Source = image;
But it's not woking. I think it's the Uri not set correctly. Help please.
This works:-
BitmapImage image = new BitmapImage(new Uri("/MyLib;component/my.png", UriKind.Relative));
MyIcon.Source = image;
I can't see why you would want to use a Stream here. Having said that your Stream code should work. The build action on the png should be "Resource" and "MyLib" in your Uri should be the Assembly name of the library as found on the "Silverlight" tab of the project properties.
Do you have your image marked as "Resource" in the properties window, or "Content"?
You could always set a style as a resource in your application and then call it like:
Application.Current.Resources["myCoolStyle"] and apply that to the image.

Resources