Hi i need to draw an checkbox within the rectangle.is there any predefined method in c# WPF .or how to i acheive this senario in WPF.
You may try this:
CheckBox checkbox = new CheckBox();
checkbox.Content = "Content";
checkbox.Height = 50;
checkbox.Width = 100;
checkbox.IsChecked = true;
checkbox.HorizontalAlignment = HorizontalAlignment.Left;
VisualBrush vb = new VisualBrush(checkbox);
drawingContext.DrawRectangle(vb, null, new Rect(50, 50, 100, 50));
1.) Create the Rectanle
2.) Create the checkbox
3.) use rectangle.Controls.Add(checkbox)
Hope this helps :)
Related
I'm working on a WPF App, and I would like to create the entire window from c# code, instead of from XML.
I tried the following code, but nothing happens, the grid is not displayed. Did I miss something? Is it possible to do it this way or is there any other solution?
public MainWindow()
{
Grid grd = new Grid();
grd.Margin = new System.Windows.Thickness(10, 10, 10, 0);
grd.Background = new SolidColorBrush(Colors.White);
grd.Height = 104;
grd.VerticalAlignment = System.Windows.VerticalAlignment.Top;
grd.ColumnDefinitions.Add(new ColumnDefinition());
grd.ColumnDefinitions.Add(new ColumnDefinition());
grd.ColumnDefinitions.Add(new ColumnDefinition());
RowDefinition row = new RowDefinition();
row.Height = new System.Windows.GridLength(45);
grd.RowDefinitions.Add(row);
row = new RowDefinition();
row.Height = new System.Windows.GridLength(45);
grd.RowDefinitions.Add(row);
row = new RowDefinition();
row.Height = new System.Windows.GridLength(45);
grd.RowDefinitions.Add(row);
InitializeComponent();
}
Grid grd was created, but not added to Window.
InitializeComponent();
this.Content = grd;
it will replace all content which was declared in XAML (if any).
However, Grid is a Panel and doesn' have visual representation itself, so window with Grid without child element will still look empty. Try grd.ShowGridLines = true; to see rows and columns
Grid documentation shows a large example actually, with equivalent c# code and xaml markup
Is it possible to set for each side of a border its own EventHandler for mouse-enter or mouse-leave event. For example for the Left-Border of a Grid and Top-Border of a Grid?
What I am actually trying to do is allow the user to resize Grid-Elements inside a Canvas that contain a TextBlock with the mouse.
I am inserting my Grid/Border into the Canvas with the following code:
Border border = new Border();
border.BorderThickness = new Thickness(2);
border.BorderBrush = Brushes.Black;
TextBlock tb = new TextBlock();
tb.HorizontalAlignment = HorizontalAlignment.Stretch;
tb.TextWrapping = TextWrapping.Wrap;
tb.Padding = new Thickness(5, 5, 5, 5);
tb.Text = fd.LabelText;
Grid grid = new Grid();
grid.Background = labelBackgroundBrush;
grid.Background.Opacity = myOpactiy;
border.DataContext = fd;
grid.Children.Add(tb);
border.Child = grid;
I found a good example at csharphelper.com . Although my implementation is still buggy this was a good inspiration for me. Maybe it can help others who want to do the same.
how can I set the style in app.xaml to my button in c# ?
there is a style for buttons in app.xaml and I want to use it in my button in c#.
here is my code for the button.
Button deleteButton = new Button();
deleteButton.Background = new ImageBrush
{
ImageSource ="Delete2.png"
};
deleteButton.Height = 70;
deleteButton.Width = 70;
deleteButton.HorizontalAlignment = HorizontalAlignment.Left;
deleteButton.VerticalAlignment = VerticalAlignment.Bottom;
deleteButton.Margin = new Thickness(0,10,0,-5);
deleteButton.BorderBrush = null;
deleteButton.Visibility = Visibility.Collapsed;
deleteButton.Click += new RoutedEventHandler(ItemDeleteButton_Click);
deleteButton.Tag = tag;
deleteButton.Style = ?????????????????
Try this code
deleteButton.Style = App.Current.Resources["StyleKey"] as Style;
You can use FindResource or TryFindResource.
Resources and Code - MSDN
I solved it
deleteButton.Style = App.Current.Resources["ButtonStyle"] as Style;
Your Style is probably defined in a ResourceDictionary elsewhere in your application. You can retrieve that using the FindResource method:
deleteButton.Style = (Style)this.FindResource("MyCustomButtonStyle");
I have a Listview and it has one column. The view is set to List and I can see each Listviewitem but I can't select the item's row, I have to select the item's text. Is it possible to make it so that the Listviewitem's width is the same size as the Listview itself so that the user can click anywhere on the Listviewitem to select the item?
I tried searching but could only find how to change the column width and how to fix it in XAML, but this is for a WinForm.
Edit - As requested, this is the code that is generated by the Visual Studio designer. It is the only code involved with the ListviewItem.
System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("1");
System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("2");
System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem("3");
System.Windows.Forms.ListViewItem listViewItem4 = new System.Windows.Forms.ListViewItem("4");
System.Windows.Forms.ListViewItem listViewItem5 = new System.Windows.Forms.ListViewItem("5");
System.Windows.Forms.ListViewItem listViewItem6 = new System.Windows.Forms.ListViewItem("6");
System.Windows.Forms.ListViewItem listViewItem7 = new System.Windows.Forms.ListViewItem("7");
this.listView1 = new System.Windows.Forms.ListView();
this.listView1.Activation = System.Windows.Forms.ItemActivation.OneClick;
this.listView1.AllowDrop = true;
this.listView1.AutoArrange = false;
this.listView1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.listView1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.listView1.ForeColor = System.Drawing.SystemColors.MenuHighlight;
this.listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
listViewItem1.StateImageIndex = 0;
this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
listViewItem1,
listViewItem2,
listViewItem3,
listViewItem4,
listViewItem5,
listViewItem6,
listViewItem7});
this.listView1.Location = new System.Drawing.Point(105, 129);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(157, 475);
this.listView1.TabIndex = 4;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.List;
A simple solution will be using a ListBox instead of List View
In case of simple text, it is better to use ListBox.
Update:
Simplest hack for getting this functionality in ListView will be adding dummy spaces at the end of each item string to fill.
Better ListView and Better ListView Express (free) support this. It behaves like a ListBox by default (Details view, no columns):
The item auto sizing can be triggered by setting AutoSizeItemsInDetailsView property to true:
betterListView.AutoSizeItemsInDetailsView = true;
Set listView1.FullRowSelect = true
There is a lot of already answered question and examples about how to draw images within combobox. But I haven't found any examples how to draw animations within combobox.
The gif animation I use is (it's transparent):
And the result I want to achieve is somethink like this:
I'm using Windows Forms and .Net 3.5.
All ways of achieving that, I thought about, were:
1. Use Graphics.DrawImage in ComboBox's DrawItem handler. But the image was drawn statically, there was no anmation.
2. Use PictureBox to show animation and then somehow resize it and place over the ComboBox.
For second soultion I used the following code:
pictureBox1 = new PictureBox();
pictureBox1.Image = Resource.myImage;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
//3 is used just for try to fit image into "white" area of ComboBox
pictureBox1.ClientSize = new Size(comboBox1.Size.Height-3, comboBox1.Size.Height-3);
pictureBox1.BackColor = System.Drawing.Color.Transparent;
pictureBox1.Dock = DockStyle.Left;
pictureBox1.Parent = this.comboBox1;
pictureBox1.Enabled = true;
pictureBox1.Visible = true;
But in result I've got this:
It's animated, but picturebox is drawn on ComboBox edges and it looks bad.
So, can anyone give me an advice or some help to achive this?
Thank you.
EDIT:
My final solution that worked:
pictureBox1 = new PictureBox();
pictureBox1.Image = Resource1.myImage;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.ClientSize = new Size(comboBox1.Size.Height - SystemInformation.Border3DSize.Height, comboBox1.Size.Height - (2 * SystemInformation.Border3DSize.Height));
pictureBox1.BackColor = System.Drawing.Color.Transparent;
pictureBox1.Location = new Point(SystemInformation.Border3DSize.Width, SystemInformation.Border3DSize.Height);
pictureBox1.Parent = this.comboBox1;
pictureBox1.Enabled = true;
pictureBox1.Visible = true;
Thank you all! You help me a lot!
Try this:
pictureBox1 = new PictureBox();
pictureBox1.Image = Resource.myImage;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
//2 is used just for try to fit image into "white" area of ComboBox
pictureBox1.ClientSize = new Size(comboBox1.Size.Height - 2, comboBox1.Size.Height - 2);
pictureBox1.BackColor = System.Drawing.Color.Transparent;
pictureBox1.Left = 1;
pictureBox1.Top = 1;
pictureBox1.Parent = this.comboBox1;
pictureBox1.Enabled = true;
pictureBox1.Visible = true;
remove the code that sets the "Dock" property. Setting this causes the layout manager to ignore size/location settings.
Instead, set the Size property and the Location property to specific values.
Something like:
pictureBox3.Size = new Size(comboBox1.Size.Height-3, comboBox1.Size.Height-3);
pictureBox3.Location = new Point(0, 3);
You may have to adjust these to get the extact position you need.
It might be then a tad too small, but it would fit, if you set the size of your combobox to pictureBox1.Size = new Size(comboBox1.ItemHeight, comboBox1.ItemHeight).
Or set the height and width to 2*SystemInformation.3DBorderSize