c# - Changing elements of a toolbar from collapsed to visible doesn't change their visibility -
i've spend whole evening trying understand , fix issue i'm encountering toolbar :
i've created tiny vector based drawing program in user can choose tool (selection, line, text, image, etc...), clicking on corresponding icon located in toolbar before using it.
when tool selected, new controls appear in toolbar (they have visibility, set collapsed, changed visible), allowing user change parameters.
for instance when clicking on text tool, field appear user can write content. when image tool selected, button appears opens openfiledialog (in order select source file), etc etc...
the problem that, in particular situation, elements should appear when corresponding tool selected, stay hidden, visibility set true in code. , missing controls not in overflow part of toolbar either.
it's pretty hard explain here source code example, mimics app , shows issue : create new c# wpf project, copy following code in mainwindow.xaml
<window x:class="ddi.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="ddi" height="250" width="318"> <dockpanel width="auto" height="auto" lastchildfill="true"> <stackpanel dockpanel.dock="top" toolbartray.islocked="true"> <menu> <menuitem header="fichier"> <menuitem header="nouveau" /> <menuitem header="ouvrir" /> <separator /> <menuitem header="quitter" /> </menuitem> </menu> <toolbar name="toolbar"> <togglebutton name="tool_selection" height="16" width="16" click="tool_selection_click" background="yellow"> </togglebutton> <togglebutton name="tool_text" height="16" width="16" click="tool_text_click" background="red"> </togglebutton> <togglebutton name="tool_image" height="16" width="16" click="tool_image_click" background="blue"> </togglebutton> <separator name="tool_separator1" margin="4,2" /> <textblock name="tool_name_caption" verticalalignment="center" margin="0,0,4,0" visibility="collapsed"> name : </textblock> <textbox name="tool_name_field" width="80" borderbrush="#ff6f6f6f" visibility="collapsed"> </textbox> <textblock name="tool_content_caption" verticalalignment="center" margin="4,0,4,0" visibility="collapsed"> content : </textblock> <textbox name="tool_content_field" width="100" borderbrush="#ff6f6f6f" visibility="collapsed"> text </textbox> <textblock name="tool_source_caption" verticalalignment="center" margin="4,0,4,0" visibility="collapsed"> source : </textblock> <textbox name="tool_source_field" width="80" borderbrush="#ff6f6f6f" isreadonly="true" allowdrop="false" visibility="collapsed"> </textbox> <button name="tool_source_select" borderbrush="#ff6f6f6f" visibility="collapsed"> ... </button> <textblock name="tool_z_caption" verticalalignment="center" margin="4,0,4,0" visibility="collapsed"> z : </textblock> <textbox name="tool_z_field" width="30" borderbrush="#ff6f6f6f" visibility="collapsed"> 0 </textbox> </toolbar> </stackpanel> <scrollviewer horizontalscrollbarvisibility="visible"> <canvas name="workspace" background="white"> </canvas> </scrollviewer> </dockpanel>
and following lines in corresponding *.cs file :
using system.windows; namespace ddi { public partial class mainwindow : window { public mainwindow() { initializecomponent(); selectselectiontool(); } private void tool_selection_click(object sender, routedeventargs e) { selectselectiontool(); } private void tool_text_click(object sender, routedeventargs e) { selecttexttool(); } private void tool_image_click(object sender, routedeventargs e) { selectimagetool(); } private void selectselectiontool() { tool_selection.ischecked = true; tool_text.ischecked = false; tool_image.ischecked = false; tool_separator1.visibility = visibility.collapsed; tool_name_caption.visibility = visibility.collapsed; tool_name_field.visibility = visibility.collapsed; tool_content_caption.visibility = visibility.collapsed; tool_content_field.visibility = visibility.collapsed; tool_source_caption.visibility = visibility.collapsed; tool_source_field.visibility = visibility.collapsed; tool_source_select.visibility = visibility.collapsed; tool_z_caption.visibility = visibility.collapsed; tool_z_field.visibility = visibility.collapsed; } private void selecttexttool() { tool_selection.ischecked = false; tool_text.ischecked = true; tool_image.ischecked = false; tool_separator1.visibility = visibility.visible; tool_name_caption.visibility = visibility.visible; tool_name_field.visibility = visibility.visible; tool_content_caption.visibility = visibility.visible; tool_content_field.visibility = visibility.visible; tool_source_caption.visibility = visibility.collapsed; tool_source_field.visibility = visibility.collapsed; tool_source_select.visibility = visibility.collapsed; tool_z_caption.visibility = visibility.visible; tool_z_field.visibility = visibility.visible; } private void selectimagetool() { tool_selection.ischecked = false; tool_text.ischecked = false; tool_image.ischecked = true; tool_separator1.visibility = visibility.visible; tool_name_caption.visibility = visibility.visible; tool_name_field.visibility = visibility.visible; tool_content_caption.visibility = visibility.collapsed; tool_content_field.visibility = visibility.collapsed; tool_source_caption.visibility = visibility.visible; tool_source_field.visibility = visibility.visible; tool_source_select.visibility = visibility.visible; tool_z_caption.visibility = visibility.visible; tool_z_field.visibility = visibility.visible; } } }
now launch application. not try resize now, in order see issue !
the yellow tool 1 selected. proceed in order : click on red button : new elements appear. click on blue button : elements stay, disappear. oddly missing : "source :" texblock, tool_source_file textbox , "..." button. try resize window : 3 missing controls appear magic (they not present in overflow part of toolbar, wasn't active before)
another way see issue :
- launch app.
- the yellow tool 1 selected.
- click on red button.
- click on blue button.
- click on red button.
- click on blue button : 3 missing controls appear in right place !
my question simple : what's going on ?
i've tried lot of things including using different version of invalidate toolbar after changing items visibility, splitting toolbar in 2 panels, etc. , nothing worked.
thanks answers.
i try code shortly tony.
i've submited issue on microsoft website, apparently don't @ suggestions unless 5 votes. please, vote : https://connect.microsoft.com/wpf/feedback/details/638552/changing-visibility-of-toolbar-components-to-visible-does-not-make-them-appear-event-in-the-overflow-part-of-the-toolbar#details
regards
i don´t know, seems bug. now, can try solve problem:
private void tool_image_click(object sender, routedeventargs e) { selectimagetool(); changewidthandrefresh(); // call make toolbar show correct controls... :) } // temp workaround weird bug. void changewidthandrefresh() { var old_width = this.width; this.width = old_width - 1; this.refresh(); this.width = old_width; this.refresh(); }
note: refresh() method extension method use on projects:
// 05-01-2010 public static class wpfuirefreshextensionmethods { // www.tonysistemas.com.br private static system.action emptydelegate = delegate() { }; /// <summary> /// força redesenhar /// mesmo que esteja dentro de um loop /// </summary> /// <param name="uielement"></param> public static void refresh(this system.windows.uielement uielement) { //uielement.updatelayout(); uielement.dispatcher.invoke(system.windows.threading.dispatcherpriority.render, emptydelegate); } }
Comments
Post a Comment