Xaml
<ScrollViewer x:Name="_ScrollViwer" />
Code behind
Image image = new Image();
_ScrollViwer.Children.Add(image);
Error
ScrollViewer' does not contain a definition for 'Children' and no accessible extension method Children'
Injecting UI control into ScrollViewer programmatically
ScrollViewer is not Panel, if you want to add Image into ScrollViewer, please pass it to ScrollViewer's content. And if you want to add more controls into ScrollViewer, you need use panel to package elements.
For example
Image image = new Image();
Image image1 = new Image();
var stackpanel = new StackPanel()
{
Children =
{
image,
image1
}
};
_ScrollViwer.Content = stackpanel;
Related
I am adding image to the radtreeviewitem from resources programatically using the below code.
"/myAssembley;component/Resources/image1.png"
and the image is displaying successfully. Now i need to add another image which needs to be displayed next to the first image in the radtreeviewitem.
how to achieve it.?
Like the below image i need my treeviewitem to display a folder icon and a red square icon in a single treeview item.
If you do not have data binding and you are using RadTreeViewItems directly you can add the additional image in the Header of the item. For example:
var stackPanel = new StackPanel() { Orientation = System.Windows.Controls.Orientation.Horizontal };
var image1 = new Image() { Source = image1Path };
var image2 = new Image() { Source = image2Path };
var textBlock = new TextBlock() { Text = itemHeader };
stackPanel.Children.Add(image1);
stackPanel.Children.Add(image2);
stackPanel.Children.Add(textBlock);
var treeViewItem = new RadTreeViewItem()
{
Header = stackPanel,
};
It Works.
The proper way would be to create a DataTemplate with a grid or horizontal stackpanel. Put two images inside and in your model two Image Sources that you can bind too. Telerik doesn't have the best track record using the MVVM pattern, but the TreeView control is pretty decent with binding. If you need help with the model and the datatemplate, post some of your code here and we can work on it.
I have some text in a ScrollViewer control written in c# code, how do I wrap the text? Any solution?
Initialy I have a pop-up, inside this a stack panel and added
ScrollViewer sv = new ScrollViewer();
In this scroll viewer i put some text.
string values[]= new string[]; //(example)
sv.Content = values[1];
When I open pop-up, if text length is more than screen size, he need to show scroll bar.
sv.TextWrapping = TextWrapping.Wrap;
is possible or something another way?
All items ( scroll viewer, text ) I puts behind view, not in xaml (view) because my content are dynamic.
For you to be able to scroll anything inside a scrollviewer, you have to stop the scrollviewer from growing with the child element.
If this happens dynamically, give the scrollviewer a certain height and the text inside it will be scrollable
Create a TextBlock, assign the text to this TextBlock and add this TextBlock as the content of the ScrollViewer
var sv = new ScrollViewer();
var tb = new TextBlock
{
Text = //your text,
TextWrapping = TextWrapping.Wrap
}
sv.Content = tb;
I want to create a button in my windows 8 desktop app, which will have an image and a text block. I have to do this using C# coding.
My code is as follows,
Button btn = new Button();
StackPanel btnContentPanel = new StackPanel();
btnContentPanel.Orientation = Orientation.Horizontal;
Image img = new Image();
img.Source = new BitmapImage(newUri(#"C:\Users\Desktop\Images\download.jpg"));
img.Stretch = Stretch.Uniform;
btnContentPanel.Children.Add(img);
TextBlock txBlock = new TextBlock();
txBlock.Text = "My Button";
btnContentPanel.Children.Add(txBlock);
btn.Content = btnContentPanel;
This is not giving any error but the image is not getting displayed. If I add another text block in place of the image, then its appearing, but not the image.
Am I missing anything ? Please help, thank you.
Try building your button like this:
Button btn= new Button
{
Width = 30,
Height = 30,
Content = new Image
{
Source = new BitmapImage(#"C:\Users\Desktop\Images\download.jpg"))
}
};
In the case of a 'missing' image there are several things to consider:
When Xaml can't locate a resource it might ignore it (when it won't throw a XamlParseException)
The Resource must be properly added and defined:
make sure it exists in your project where expected.
Make sure it is built with your project as a Resource.
(Right click -> Properties -> BuildAction='Resource')
Another thing to try in similar cases, which is also useful for reusing of the image (or any other resource):
Define your Image as a Resource in your Xaml:
<UserCondrol.Resources>
<Image x:Key="MyImage" Source.../>
</UserControl.Resources>
And later use it in your desired control/controls:
<Button Content={StaticResource MyImage} />
I create a user control dynamically in my code
UserControl myobject = new UserControl();
myObject contains a button etc.
when I add this control to my picturebox
picturebox.Controls.Add(myobject);
my picturebox's backgorund image is dissappeared.
Why?
note: the button can be seen however. I want picturebox to be seen also
Set transparent basckground color of your user control. This will make picturebox visible:
UserControlDisplay myobject = new UserControlDisplay();
myobject.BackColor = Color.Transparent;
picturebox.Controls.Add(myobject);
BTW I believe you have different name of user control. And yes, as #samjudson stated, PictureBox should not be used this way: try to use Panel with background image instead (approach will stay same - use transparent color to see parent control):
panel.BackgroundImage = // your image
UserControlDisplay myobject = new UserControlDisplay();
myobject.BackColor = Color.Transparent;
panel.Controls.Add(myobject);
Try this:
UserControl myobject = new UserControl();
Button but = new Button();
but.BackColor = Color.Gray
pic.BackColor = Color.Green;
myobject.Controls.Add(but);
pic.Visible = true;
pic.Controls.Add(myobject);
The PictureBox control is not meant to be used as a container. Try adding a parent Panel or similar and the adding the PictureBox and your custom control to the Panel control.
I want to add an Image dynamically to the wpf application.
What I have tried :
Image img = new Image();
img.Height = 100;
img.Width = 100;
img.Visibility = Visibility.Visible;
Thickness th = new Thickness(100, 100, 0, 0);
img.Margin = th;
string strUri2 = String.Format(#"pack://application:,,,/MyFirstWPF;component/Images/tt.jpg");
img.Source = new BitmapImage(new Uri(strUri2));
I know, the image won't display unless I add it to the Content.
this.Content=img;
but with this the existing controls(shapes) on the app are lost.
Now, my question is how to add the image to the content without losing existing controls from the app.
When you are going to load and show an image dynamically, you still need to think about the layout of your application. I would suggest to add an (initially empty) Image control in XAML (for example in a Grid), and later set the Source property of this control in code.
<Grid>
... other controls ...
<Image Name="img" Grid.Row="..." Grid.Column="..."
Width="100" Height="100" Margin="100,100,0,0"/>
</Grid>
Set Source in code:
var uri = new Uri("pack://application:,,,/MyFirstWPF;component/Images/tt.jpg");
img.Source = new BitmapImage(uri);
by default the window content is a grid so try
(this.Content as Grid).Children.Add(img);