So I am trying to build a usercontrol that may or may not have children to it. I know C# but still learning a lot about the XAML side of things.
What I want to do is something like:
IF Children.Count > 0 then Create TreeView
ELSE Create Label
I know how to create a single view but I don't know how to create a view based on conditions like this. Thanks for the help!
A "no Code questions" is rewarded by a "no Code answer" ;o)
Put a treeview in your "usercontrol"
Put your Label in your "usercontrol"
Bind the Visibility property of both to your Collection Holding your "Children".
Create two different (or one parametrized) IValueConverter-Implementing-Converter and use that as "Converter=...." in your Binding.
Make the one converter return Visibility.Hidden when you have less than two childs, make the second return Visibility.Hidden when you have zero or one child.
Put the correct Converter to each of your Bindings so the Label is Hidden when more than one Child is in your Collection and the TreeView is hidden if less than two Childs are in the Collection.
Rethink Building a full fleshed "usercontrol" for this ...
Related
I'm pretty new to WPF/XAML in C#, I saw some simple tutorials and stuff but, today I'm confused, never used to make UI there.
I need to output multiple forms which instance would look something like that:
(NB: I constructed it just in WYSIWG way, so XAML markup is total mess, it for screenshot. I cant even add table directly in grid, oh)
I got data like this for each month in schoolyear, so, depends on what class is choosen I want it to output like that (scheme)
(NB: instead of each CustomWindowInstance there I need my form from 1 st screenshot)
So, whats is best and simplies approach for that?
EDIT 1: I'll name XAML from the first screenshot, which represents controls you want to display multiple times, XAML 1, and another one, where you want to do it, XAML 2.
--
Almost ORIGINAL:
Create a separate User Control and move the content of Window tag from XAML 1 into it. Then you'll be able to show it as many times as you want by placing <yourUserControlNamespace:YourUserControlClass /> in XAML 2.
For an ability of communication with this control in XAML like <yourUserControlNamespace:YourUserControlClass Parameter="SomethingHere" /> you should implement a dependency property with name ParameterProperty (desired name of property in XAML + "Property") in YourUserControlClass.xaml.cs.
--
EDIT 2: One may also create a DataTemplate with parameter DataType={x:Type yourVMNamespace:YourVMClass} for the XAML 1's ViewModel (not View) in XAML 2's or the whole application's Resources. In that case the ViewModel class you are binding to will be represented by the DataTemplate's content itself. Later you can write <yourVMNamespace:YourVMClass /> as in the first solution.
This approach allows displaying rich representation of any items themselves, for example, in controls derived from ItemsControl, such as ListBox, all you've got to do is to bind ListBox's ItemsSource property to a collection of YourVMClass instances (or specify them in XAML by hand).
More precisely, in this case I won't call YourVMClass a ViewModel.
I have two ObservableCollections in my ViewModel. Both of same generic type.
Window has two ItemsControls each with ItemsSource bound on its own ObservableCollection from ViewModel.
Both use same same ItemTemplate. This ItemTemplate is very complex user control.
It's all done by the book.
In some cases, I move item from one collection to another. UI updates correctly. However, as control is complex, it needs about 1-2sec to render when it is recreated in new ItemsControl. And since this happens on drag and drop operation, it is not user friendly.
What I would like to do is to somehow preserve existing instance of ItemTemplate from source ItemsControl and reuse it in destination. I don't know if it is even possible, I couldn't find anything about it online.
I believe setup is fairly simple, but if needed I'll write some simplified version of code (I can't share the real code).
Thanks for any help.
What I would like to do is to somehow preserve existing instance of ItemTemplate from source ItemsControl and reuse it in destination. I don't know if it is even possible, I couldn't find anything about it online.
I am afraid it is not. When you disconnect an element from the visual tree and then add it back to a different parent element at a later stage, all elements that make up the control will be re-rendered.
I am working with Expression Blend 3. I want to do something and I do not know if it is possible but common sense tells me it should be.
The whole idea is to break apart a XAML content into three XAML pages. One page is the master that contains controls defined in two seperate pages. That is it. Can it be done?
Add this to the child controls to be nested:
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:SWECommonControls="clr-namespace:SWECommonControls;assembly=SWECommonControls"
I am creating a custom ChildWindow that I want to use with a DataTemplate.
The DataTemplate will apply to the "body" of the window, but then, separate from that, I want to always display two buttons, "Save" and "Cancel".
I have no idea how to accomplish that... Any help would be greatly appreciated!
Grab a copy of your ChildWindowStyle from your SdkStyles.xaml to give you a foundation for building your custom control template on. To keep a DataContext you could throw it in a UserControl as UserControl.Resources or if you're just populating ContentPresenters etc you can put the template in your own resource dictionary or wherever you like (though you might want to specify a unique x:Key name for it.) Just depends on how you'd like to use it.
Make your desired changes to the template and also add your Buttons etc. Then you can either set it as the default by replacing the Default BasedOn value in your resource dictionary to point to it or call that style explicitly.
Personally I prefer Expression Blend for all of this and there's even some tutorials out there to help you along with a Web Search (which I might suggest first next time.) Like what you might find here... Hope this helps! :)
Suppose I have an element (in my case, a StackPanel) that contains several UI elements (in my case, lots of textboxes contained in various Grids contained in etc.etc. contained in the StackPanel).
I want to know whether any one of those textboxes has focus. (I want to bind this property to a View-Model property.) Is there a property for this? If not, what is the simplest way to bind to this kind of information, without having to first extract all the textboxes? (They’re generated by templates.)
You could use IsKeyboardFocusWithin. What kind of binding are you wanting to do to it? If it's something simple like you're wanting to change the background of the stackpanel if a textbox within has focus, you should be able to use this as a style trigger.