I am using a FrameworkElementFactory as the following code in my WPF program:
FrameworkElementFactory txt1 = new FrameworkElementFactory(typeof(TextBox));
I tried to programmatically focus on this FrameworkElementFactory and also tried it with VisualTreeHelper.HitTest. Is there any way to focus on *txt1 * like txt1.Focus or HitTest ?
I created it on Listview and filled in its columns programmatically. And design this Listview according to the following codes:
for (int x = 0; x <= obj.ClmnDegerler.Length - 1; x++)
{
var GridViewColumn1 = new GridViewColumn();
txt1 = new FrameworkElementFactory(typeof(TextBox));
txt1.SetValue(TextBox.HeightProperty, 20.0);
txt1.SetValue(TextBox.WidthProperty, clmnwidth);
dataTemplate = new DataTemplate();
dataTemplate.VisualTree = txt1;
GridViewColumn1.CellTemplate = dataTemplate;
this.UGridview1.Columns.Add(GridViewColumn1);
}
And I created a button by xaml and when I want to click the button I want to focus on txt1
private void Button_Click(object sender, RoutedEventArgs e)
{
//txt1.Focus(); not working
}
Related
In certain cases I have the problem that the FontSize that I set on a WPF window is not inherited to a child control.
It happens, if a custom user control sets its content (e.g. a Label) upon changing the DataContext.
I can reproduce this when putting this UserControl into a new window, then close this window and create a new one having the same UserControl in it (see the following code).
In my complex application it's a custom popup window and a custom UserControl that changes its content if the DataContext is changed. There the font is not inherited upon the first open of the window (so the usercontrol hasn't been in another visual/logical tree until that) but I can't reproduce this in a small test application.
public partial class App : Application
{
// App.xaml: ShutdownMode="OnExplicitShutdown"
private void Application_Startup(object sender, StartupEventArgs e)
{
var testControl = new TestControl();
var w = new Window();
w.FontSize = 40;
w.DataContext = this;
w.Content = testControl; // TestControl.DataContextChanged creates label which has FontSize = 40
w.Show();
w.Close();
w.DataContext = null;
//w.Content = null; // if this is done, the font will be correct (40)
w = null;
w = new Window();
w.FontSize = 40;
w.DataContext = this;
//testControl.DataContext = this; // if this is done, the font will be correct (40)
w.Content = testControl; // TestControl.DataContextChanged creates label with remaining FontSize = 12 (Default)
w.Show();
}
}
public class TestControl : UserControl
{
public TestControl()
{
DataContextChanged += TestControl_DataContextChanged;
}
private void TestControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue != null) Content = new Label() { Content = "TestControllabel"};
else Content = null;
}
}
I'm not looking for a fix of this example app, but for the reason why the font size is not inherited in this special case, so maybe then I can fix my complex app.
Any thoughts would be useful !
Edit: For now I fixed my application by setting the datacontext of the control before setting it as window content.
I need to Align Text for ComboBox by left, right or center. I couldn't find TextAlignment or HorizontalAlignment property for ComboBox in WinForms.
How can i set TextAlignment for ComboBox?
public partial class Form1 : Form
{
ComboBox comboBox;
public Form1()
{
InitializeComponent();
var button = new Button() { Text = "Increase", Size = new Size(100, 20), Location = new Point(10, 10) };
button.Click += button_Click;
this.Controls.Add(button);
comboBox = new ComboBox() { Location = new Point(100, 100), Size = new Size(200, 20), MinimumSize = new Size(0, 0), Font = new Font("Calibri", 11), Text = "Stack Overflow" };
comboBox.Items.Add("One");
comboBox.Items.Add("Two");
comboBox.Items.Add("Three");
comboBox.DrawMode = DrawMode.OwnerDrawVariable;
comboBox.DrawItem += comboBox_DrawItem;
this.Controls.Add(comboBox);
}
void comboBox_DrawItem(object sender, DrawItemEventArgs e)
{
// By using Sender, one method could handle multiple ComboBoxes
ComboBox cbx = sender as ComboBox;
if (cbx != null)
{
// Always draw the background
e.DrawBackground();
// Drawing one of the items?
if (e.Index >= 0)
{
// Set the string alignment. Choices are Center, Near and Far
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
// Set the Brush to ComboBox ForeColor to maintain any ComboBox color settings
// Assumes Brush is solid
Brush brush = new SolidBrush(cbx.ForeColor);
// If drawing highlighted selection, change brush
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
brush = SystemBrushes.HighlightText;
// Draw the string
e.Graphics.DrawString(cbx.Items[e.Index].ToString(), cbx.Font, brush, e.Bounds, sf);
}
}
}
}
Above code only sets my combobox item to be displayed in center. How can the text displayed in the combobox be set to center alignment? My goal is to align that text box only not the dropdown items.
Thanks in Advance.
Regards,
Venkatesan R
For completeness: Just as we discussed in comments, the code you have successfully aligns the ComboBox items but for the editing TextBox to align you must add the following line, according to the originally linked blog:
comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
For the issue at hand:
[M]y goal is to align that text box only not the dropdown items.
Simply wrap your alignment lines of code in the DrawItem method in the following if-statement:
if ((e.State & DrawItemState.ComboBoxEdit) == DrawItemState.ComboBoxEdit)
{
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
}
A ListBox and a ContextMenu are created dynamicaly. The ListBox has some items.
How do I know the ListBoxItem Text that right mouse button clicked on?
private void Init2()
{
ContextMenu contextMenu = new ContextMenu();
MenuItem menuItemOpen = new MenuItem();
menuItemOpen.Click += new RoutedEventHandler(menuItemOpen_Click);
contextMenu.Items.Add(menuItemOpen);
listBox1.ContextMenu = contextMenu;
}
void menuItemOpen_Click(object sender, RoutedEventArgs e)
{
//How do I know the listItem text that right mouse button clicked on?
}
When you right click, you actually also select. So that means you can just do:
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
string selectedListBoxItemText = ((ListBoxItem)listBox1.SelectedItem).Content.ToString());
// do your thing
}
I want to set an image as background by clicking the ctrl button?
Any one have ideas? please revert me ASAP.
Thanks in advance.
On button click event apply the following code
ImageBrush myBrush = new ImageBrush();
myBrush.ImageSource =
new BitmapImage(new Uri(#"Image Path"));
this.Background = myBrush;
You can replace the this with your control name
You can try the following code:
private void button1_Click(object sender, RoutedEventArgs e)
{
BitmapImage imageSource = new BitmapImage(new Uri(#"Path/to/image.jpg"));
ImageBrush brush = new ImageBrush();
brush.ImageSource = imageSource;
grid.Background = brush;
}
For the wpf listview , in the Mouse Over event how do i get a reference to the item that the mouse cursor is on ?
Regards,
MadSeb
You have to use the MouseOver event from the listViewItem that the mouse is over, not the one from the listview itself.
public MainWindow() {
InitializeComponent();
ListView listView = new ListView();
ListViewItem listViewItem = new ListViewItem();
listViewItem.MouseMove += myMouseMoveEvent;
listView.Items.Add(listViewItem);
}
private void myMouseMoveEvent(object sender, MouseEventArgs e) {
ListViewItem item = (ListViewItem) sender;
// now you can handle the events with this item....
}