text doesnt appear on textbox - c#

i have combobox, that should display Text property as TextBlock.Text inside it.
no errors no warnings just no text at combobox.
private void ComboBoxTemplates_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is ComboBox)
{
//string text;
ComboBox comboBox = sender as ComboBox;
if (comboBox.SelectedIndex < 0)
{
e.Handled = true;
return;
}
StackPanel stackPanel = comboBox.Items[comboBox.SelectedIndex] as StackPanel;
TextBlock senderTextBlock = stackPanel.Children[0] as TextBlock;
GlobalSettings.TemplateIndex = comboBox.SelectedIndex;
comboBox.SelectionChanged -= ComboBoxTemplates_OnSelectionChanged;
//text = senderTextBlock.Text;
comboBox.Text = "asd";
comboBox.SelectionChanged += ComboBoxTemplates_OnSelectionChanged;
TemplateApplier.ComboBoxTemplates_OnSelectionChanged(sender, e);
}
}
i'm expecting to "asd" appear on top of combobox.

Related

C# WPF TextBox not focusing

I made a method what adds a TextBox to a Grid, but when i set it to focus it just won't.
This is the code what makes the textBox, and adds to the grid:
public static TextBox TextEditor(this FrameworkElement obj, Grid parent)
{
Label text = obj as Label;
TextBox edit = new TextBox();
edit.Text = text.Content.ToString();
edit.VerticalContentAlignment = VerticalAlignment.Center;
edit.Margin = obj.Margin;
edit.SetSize(obj);
edit.HorizontalAlignment = HorizontalAlignment.Left;
edit.VerticalAlignment = VerticalAlignment.Stretch;
edit.FontSize = 15;
parent.Children.Add(edit);
edit.KeyDown += (s, e) =>
{
if(e.Key == Key.Enter)
{
text.Content = edit.Text;
parent.Children.Remove(edit);
}
if(e.Key == Key.Escape)
{
parent.Children.Remove(edit);
}
};
parent.SizeChanged += (s, e) =>
{
edit.SetSize(obj);
};
edit.Focusable = true;
return edit;
}
Here the code what calls the TextEditor method and set the textbox in focus, or at least tries it:
private void Type_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
Type.TextEditor(PreviewItems).Focus();
}
The Type variable is a Label. I searched a lot but couldn't find a solution. Why doesn't it work?
I tried every method in this post:
WPF: Can't get my control to take focus

Combobox passed as a Control template then as a popup

I have a combobox "recent_users" as a control template and then as a poppup. How can I pass the selected value of the popup to the method below?(popup click)
recent_users.SelectedItem.ToString() always returns null.
private void usernameEnter_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
Trace.WriteLine("testing dropdown");
ControlTemplate ct = recent_users.Template;
Popup popup1 = ct.FindName("PART_Popup", recent_users) as Popup;
if (popup1 != null)
{
popup1.Placement = PlacementMode.Top;
}
recent_users.IsDropDownOpen = true;
popup1.PreviewMouseUp += new MouseButtonEventHandler((s,e) => popupClick(s,e,recent_users.SelectedItem.ToString()));
}
private void popupClick(object sender, MouseButtonEventArgs e, String recent)
{
usernameEnter.Text = recent;
Trace.WriteLine("appending norms");
}
}
}
Isn't it because the user has not made any selection in the ComboBox? I have re-written the code slightly
private void usernameEnter_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
ControlTemplate ct = recent_users.Template;
Popup popup1 = ct.FindName("PART_Popup", recent_users) as Popup;
if (popup1 != null)
{
popup1.Placement = PlacementMode.Top;
}
recent_users.IsDropDownOpen = true;
// if none selected, set first as selected
if (recent_users.SelectedIndex < 0 && recent_users.HasItems)
{
recent_users.SelectedIndex = 0;
}
var selectedItem = recent_users.SelectedItem as ComboBoxItem;
var selectedUser = selectedItem.Content.ToString();
popup1.PreviewMouseUp += new MouseButtonEventHandler((s, e) => popupClick(s, e, selectedUser));
}
But I was wondering adding a handler for PreviewMouseUp event (in the last line) is something that you really want. I would have fetched the selected user in the SelectionChanged of the recent_users rather.

Change Cursor to Square when Combobox text is selected

I have Editable Combobox in WPF,way it should work is, if text is selected(highlighted), cursor should turn into cross so user can know he can move text into another Combobox, and if user try yo edit cursor should be edit cursor.
Here is code I am trying right now, onFocus Event,
private void LocationComboBox_GotFocus(object sender, RoutedEventArgs e)
{
ComboBox combo = (System.Windows.Controls.ComboBox)sender;
var edit = (TextBox)combo.Template.FindName("PART_EditableTextBox", combo);
var selectedText = edit.SelectedText;
if (!string.IsNullOrEmpty(selectedText))
{
Cursor = Cursors.Cross;
}
else
{
Cursor = Cursors.Arrow;
}
}
here is screen shot
As in Snap, since Austin,TX is highlighted, my cursor should be cross!
Thank you in advance!
Use following code:
private void cmb_Loaded(object sender, RoutedEventArgs e)
{
TextBox TxtBox = (TextBox)cmb.Template.FindName("PART_EditableTextBox", cmb);
if (TxtBox != null)
{
TxtBox.SelectionChanged += TxtBox_SelectionChanged;
}
}
void TxtBox_SelectionChanged(object sender, RoutedEventArgs e)
{
var TxtBox = sender as TextBox;
if (TxtBox != null && !string.IsNullOrEmpty(TxtBox.SelectedText))
{
Mouse.OverrideCursor = Cursors.Cross;
}
else
{
Mouse.OverrideCursor = Cursors.Arrow;
}
}

Get text from combobox in selected tab

In all of these tabs, I have a combobox with different functions as strings. I want the text under Preview (it's a richtextbox with "Nothing is selected." as a default string) to change whenever I select an item in each of the different tabs' comboboxes. Any idea how I can do that?
You could set every TextChanged event of all of your combobox to the same event handler
comboBox1.TextChanged += CommonComboTextChanged;
comboBox2.TextChanged += CommonComboTextChanged;
comboBox3.TextChanged += CommonComboTextChanged;
comboBox4.TextChanged += CommonComboTextChanged;
private void CommonComboTextChanged(object sender, EventArgs e)
{
ComboBox cbo = sender as ComboBox;
richTextBox.Text = cbo.Text;
}
However, if you change the DropDownStyle of your combos to ComboBoxStyle.DropDownList then you could use the SelectedIndexChanged event that will be triggered only when your user changes the item selected by using the DropDown List.
comboBox1.SelectedIndexChanged += CommonComboIndexChanged;
comboBox2.SelectedIndexChanged += CommonComboIndexChanged;;
comboBox3.SelectedIndexChanged += CommonComboIndexChanged;;
comboBox4.SelectedIndexChanged += CommonComboIndexChanged;;
private void CommonComboIndexChanged;(object sender, EventArgs e)
{
ComboBox cbo = sender as ComboBox;
richTextBox.Text = cbo.Text;
}
Finally to set the content of the RTB to the one of the combo in the current tab page you need to handle the TabChanged event of your tabControl
private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
switch(e.TabPageIndex)
{
case 0:
richTextBox.Text = comboBox1.Text;
break;
// so on for the other page and combos
}
}
Or if your comboboxes share a common initial part of their names
private void tabControl1_Selected(object sender, TabControlEventArgs e)
{
var result = e.TabPage.Controls.OfType<ComboBox>()
.Where(x => x.Name.StartsWith("cboFunction"));
if(result != null)
{
ComboBox b = result.ToList().First();
richTextBox.Text = comboBox1.Text;
}
}

Setting label text in listview control in loggedin template

I need to set a label text which is listview and the listview is in Logged in template.
I am unable to set the value of the label. Here is the below code.
ListView ListView1 = (ListView)LoginView.FindControl("ListView1");
for (int i = 0; i < ListView1.Controls.Count; i++)
{
Label someLabel = (Label)ListView1.Controls[i].FindControl("nItemsId");
if (someLabel != null)
someLabel.Text = dt.Rows.Count.ToString();
}
So I think you need to use ListView.ItemCreated Event to achieve this
protected void LV_ItemCreated(object sender, ListViewItemEventArgs e)
{
// Retrieve the current item.
ListViewItem item = e.Item;
// Verify if the item is a data item.
if (item.ItemType == ListViewItemType.DataItem)
{
Label someLabel = (Label)ListView1.Controls[i].FindControl("nItemsId");
if (someLabel != null)
someLabel.Text = dt.Rows.Count.ToString();
}
}
To use it change your markup to declared the eventHandler like this.
<asp:ListView OnItemCreated="LV_ItemCreated" />

Categories