I'm working on a project with a treeview inside a popup. I need to have it where the popup remains open until a selection is made on the treeview. At first I tried controlling this with selectedItemChanged on the treeview, then MouseLeave, but if you overshoot the popup by just a pixel, it closes without you being able to make a selection on the treeview.
I'm looking to do something such as"
if (dirTree.SelectedItem = "")
{
popup1.IsOpen = true;
}
popup1.IsOpen = false;
but that gives me "Possible unintended reference comparison; to get a value comparison, cas t the left hand side to type 'string'."
I've been searching using "treeview any selected" "treeview SelectedItem", etc. Admittedly it may just be a simple syntax issue because I'm new to C# and WPF.
Do your check to make sure its's not null and then you can do dirTree.SelectedItem.Equals("") or cast to the expected type.
Related
I have a simple user control (just an example): it is 40x100, but resizable. It has two buttons, one anchored at the top, one anchored at the bottom.
It put this control on a form and stretch it to 40x400. This works fine.
But as soon as I switch the form to Localizable = True and change the language to translate any strings, the Designer shows the user control
as if it was 40x100 for both the default an the translated language, i.e. the bottom button is not anchored.
Or better: the bottom button is displayed as if it was not anchored. The control occupies the correct amount of space (40x400), though (see selection highlight). And it displays fine during runtime, this is just a Designer issue.
A picture showing the issue.
Did I miss something here? Is this how it is supposed to work?
Im on VS2010 at the moment, tried the old VS2005 but it's the same there.
Thanks...
I could easily repro this problem by anchoring the second button to the bottom. The Anchor property has a few oddish failure modes, layout isn't always recalculated when it should be. You found one such case. I think the underlying issue is that the Size property is a localizable property as well and the designer fails to fire the required events when it starts a new localization set. Something like that, nothing very trivial.
You'll need to punt this problem and not rely on the Anchor property to get the button positioned correctly. That just takes a one-liner in your UserControl code, like:
protected override void OnResize(EventArgs e) {
button2.Top = this.ClientSize.Height - button2.Height;
base.OnResize(e);
}
I am trying to mimic the functionality of the address book in Outlook
So basically a user starts typing in some text in an edit control and a matching ListView Item is selected
private void txtSearchText_TextChanged(object sender, EventArgs e)
{
ListViewItem lvi =
this.listViewContacts.FindItemWithText(this.txtSearchText.Text,true, 0);
if (lvi != null)
{
listViewContacts.Items[lvi.Index].Selected = true;
listViewContacts.Select();
}
}
The problem with this is once the listview item gets selected the user cant keep typing into the text Box. Basically I want a way to highlight an item in the listview while still keeping the focus on the edit control
This is WINFORMS 2.0
Manually setting ListViewItem.BackColor is not good a solution, especially if you want the item to get the selected state, because it only works on unselected items. So you had to take care of several situations to make it look right in all cases (really select the item as soon as the ListView gets focus, undo the color changes, and so on...)
It seems the only good way is to use Ownerdraw or an extended ListView like ObjectListView.
I was looking for the same and I still hope for a better/smarter solution, or at least a nice and short Ownerdraw implementation.
Update
I found a better solution for me: I now use a DataGridView for the same purpose (which also has other advantages in my case, since the data comes from a db anyway, but it would also work without db). There the selection bar doesn't change color when loosing focus. You can try a few properties to make it look like a ListView:
dgv.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
dgv.ColumnHeadersVisible = false;
dgv.MultiSelect = false;
dgv.ReadOnly = true;
dgv.RowHeadersVisible = false;
dgv.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
dgv.StandardTab = true;
ok never mind it's doable by just manipulating the background colour of the selected item
I've done a lot of searching on SO and google around this problem, but can't seem to find anything else to try.
I have a MainView (window) that contains a tab control. The tab control binds to an ObservableCollection of ChildViews (user controls). The MainView's ViewModel has a method that allows adding to the collection of ChildViews, which then creates a new tab. When a new tab is created, it becomes the active tab, and this works fine. This method on the MainView is called from another ViewModel (OtherViewModel).
What I am trying to do is set the keyboard focus to the first control on the tab (an AutoCompleteBox from WPFToolkit*) when a new tab is created. I also need to set the focus the same way, but WITHOUT creating a new tab (so set the focus on the currently active tab).
(*Note that there seem to be some focus problems with the AutoCompleteBox--even if it does have focus you need to send a MoveNext() to it to get the cursor in its window. I have worked around this already).
So here's the problem. The focusing works when I don't create a new tab, but it doesn't work when I do create a new tab. Both functions use the same method to set focus, but the create logic first calls the method that creates a new tab and sets it to active. Code that sets the focus (in the ChildView's Codebehind):
IInputElement element1 = Keyboard.Focus(autoCompleteBox);
//plus code to deal with AutoCompleteBox as noted.
In either case, the Keyboard.FocusedElement starts out as the MainView. After a create, calling Keyboard.Focus seems to do nothing (focused element is still the MainView). Calling this without creating a tab correctly sets the keyboard focus to autoCompleteBox.
Any ideas?
Update:
Bender's suggestion half-worked.
So now in both cases, the focused element is correctly the AutoCompleteBox. What I then do is MoveNext(), which sets the focus to a TextBox. I have been assuming that this Textbox is internal to the AutoCompleteBox, as the focus was correctly set on screen when this happened. Now I'm not so sure. This is still the behavior I see when this code gets hit when NOT doing a create. After a create, MoveNext() sets the focus to an element back in my MainView.
The problem must still be along the lines of Bender's answer, where the state of the controls is not the same depending on whether a new tab was created or not. Any other thoughts?
Final Update
As noted, majocha's suggestion worked.
I wanted to update this in case anyone happened upon this same problem with the AutoCompleteBox. It appears that setting focus does not activate it in the UI--you need to do a MoveNext on it to move focus forward once to the control's internal Textbox. This is based on my debugging experience, which may not be 100% scientific. If I have time, I will attempt to create a small repro project and submit it to the WPFToolkit team.
You can try defering the focus change with
Dispatcher.BeginInvoke(MyChangeFocusAction, DispatcherPriority.ContextIdle);
It will get queued after layout and properties updates are done.
I don't think it's best practice, but it works for me.
The control must be visible to be focused, you may try to defer focusing by subscribing to the IsVisibleChanged event, something similar to the following should work:
public static void setFocusLate(this Control control)
{
DependencyPropertyChangedEventHandler handler = null;
handler = delegate
{
control.Focus();
control.IsVisibleChanged -= handler;
};
control.IsVisibleChanged += handler;
}
I have a Silverlight control that appears on top of another (picture a pop-up box). In this pop-up control, I have a scrollview (height = 250) with a stack panel (instance name = spMain, orientation = vertical) inside. Within the contents of the stack panel are several textboxes stacked on top of each other. When I tab from textbox to textbox, the scrollviewer automatically moves toward the bottom (I wrote code in an event handler that all textboxes are linked to that does that).
The problem I'm having is when I attempt to close out the pop-up control, I'm receiving an error stating that the value does not fall within the expected range.
if (sender is TextBox)
{
TextBox tb = (TextBox)sender;
try
{
// Code bombs out here when I attempt to close out the pop-up control
Point pt = tb.TransformToVisual(spMain).Transform(new Point());
if (pt.Y >= scrollViewerHeight - tb.ActualHeight)
{
svMain.UpdateLayout();
svMain.ScrollToVerticalOffset(scrollViewerHeight += pt.Y);
}
}
catch (ArgumentException aex)
{
// Don't want to eat the exception
string errorMessage = aex.Message;
System.Diagnostics.Debug.WriteLine(errorMessage);
}
}
I'm not surprised I'm getting the error, because it appears to make sense, but what I'm looking for is some sort of User Control Unloaded event or prevent the offending code from executing.
Does anyone have any ideas on how to go about this?
I think the problem lies with how you're closing the popup. Are you removing it from the visual tree or just setting its visibility to collapsed?
If (as I think your problem suggests) you're removing it from the visual tree entirely, you may be able to solve the immediate problem by collapsing the visibility of your text boxes first, and then removing the control. That assumes that your code above is being called as a result of a resize (or a potential resize) of the textboxes. Alternately, you could just empty them of content before you remove them also.
As for an Unloaded event, there isn't anything in the framework that'll do that for you. You could write a custom unload method for your control easily enough though, and just use that when you want to remove it.
I'm trying to show a windows forms tooltip inside a datagrid to highlight an error. The problem I have is that everytime I call tooltip.Show("You have an error", datagrid, 0, 0), The tooltip is confined within the datagrids boundaries and doesn't go outside, which ultimately means the tooltip itself covers up the actual row where the error occurs.
I thought about tooltip.Show("You have an error", Form1, ?, ?) but I don't see an easy way to compute the offset of the datagrid on the form. Since all controls are docked, depending on how the user resizes the form, the location will change.
There is a caveat, the datagrid itself is not a Forms.DataGrid, instead it is an Infragistics UltraGrid which may do funny things itself, which are outside of my ability to alter.
It turns out that it's easy enough to get the location for the Show command from the UltraGrid by querying the UIElement associated with it. Here's what I'm doing:
private void ultraGrid1_BeforeCellUpdate(object sender, BeforeCellUpdateEventArgs e)
{
if (!DataFormat.CanEdit(e.Cell.Row.ListObject, e.Cell.Column.PropertyDescriptor))
{
var tip = new System.Windows.Forms.ToolTip();
tip.BackColor = Color.Orange;
tip.Show("unable to edit", this, e.Cell.GetUIElement().Rect.Left, e.Cell.GetUIElement().Rect.Top, 500);
e.Cancel = true;
}
}
Have you looked at these:
HOWTO:Create Advanced ToolTips For The WinGrid
BeforeDisplayDataErrorTooltip Event