Expose inner controls in wpf - wpf

In wpf a ListBox has a ScrollViewer property and the intellisense shows it with a 'brackets' icon:
It allows you to navigate its inner properties:
In my custom control I want to expose an inner ListBox so I created a dependency property of type ListBox.
The result is a 'ListBox' property that shows up in the intellisense with a 'wrench' icon and non-navigable inner properties.
How can i mimic what the ListBox control does with the ScrollViewer?

ScrollViewer is a ContentControl which has these three Attached Properties
CanContentScroll
HorizontalScrollBarVisibility
VerticalScrollBarVisibility
So here ListBox sets these attached properties on itself that ScrollViewer exposes and only attached properties can be set like this.
Your case is different, you want to directly set/bind properties of child control on parent control which is not a best approach to follow. Rather you can expose properties you want you child to be bound to as dependency properties on custom control and bind child internally to those. In this way when you change those properties on custom control, child will also be updated.

Related

Is it possible to bind a XAML control to a corresponding control in the ViewModel?

I am using WPF with the intent of adhering to the MVVM model. I have multiple controls in my XAML view, such as buttons, combo boxes, etc, and I have a corresponding set of controls in my ViewModel. Is there a way to bind a control (such as a button, for example) in XAML to a matching control in the ViewModel, so they have a one-to-one relationship?
I am aware of how it is usually done, with specific XAML control properties being bound to specific properties within the ViewModel. I thought that by binding an entire control to a matching control, it would simplify the binding process. I wouldn't need to bind individual properties, I could just bind the entire control and then programmatically modify any properties I wanted to in the ViewModel.
For example, my XAML button control would look something like this (the DataBinding property is not an actual property):
<Button DataBinding="SignInButton" />
And all the properties for the button would not be set in XAML, but rather in my ViewModel:
public Button SignInButton;
SignInButton.Content = "Sign In";
SignInButton.Width = 100;
SignInButton.FontFamily = "Verdana";
Expose the controls in ViewModel as ContentControl Properties and Bind them in XAML.

Binding from usercontrol to property of parent usercontrol

Let's say I have a user control called Graph in a window. This user control contains a grid, within which there is another user control, called Toolbar. Now Graph exposes a public property called Mode, which uses a dependency property called ModeProperty as a backing store. I want an element in the Toolbar user control to bind to the Mode property in its ancestor Graph. How can I do this? Even just getting a reference to Graph is proving to be harder than I expected, this.parent gets a reference to the grid rather than the Graph.
You can bind it in xaml only like this -
<Toolbar Mode="{Binding Path=Mode, RelativeSource={RelativeSource
Mode=FindAncestor, AncestorType = UserControl, AncestorLevel =2}}"/>
Also you can use the ElementName in your binding like this -
<Toolbar Mode="{Binding Path=Mode, ElementName=GraphUserControl}"/>
You have to set x:Name property on your userControl to which you want to bind to -
<UserControl x:Name="GraphUserControl"></UserControl>
Or in case you won't to do in code behind, you can look at this useful post for travelling to ancestor parent - Visual Tree Navigator

Selecting TabItem from code-behind when tabs use a DataTemplate

I've got a TabControl in my WPF application. The TabControl's ItemsSource is bound to an ObservableCollction of view objects. It uses a DataTemplate to populate the visual tree for the tabs from the Items in the collection.
I need to be select the current tabs in the conde-behind in response to actions the user takes on another screen in the application. When I iterate over the items in the TabControl's Items collection, I get the instances of my view models.
How do I access the actual TabItems and iterate over them, then select the one I want?
Tony
If you're using a MVVM approach you should bind your TabControl's SelectedItem property to the same object that holds your ObservableCollection of TabItems (the ViewModel). When you need to change the current tab set the SelectedItem property to the correct TabItem in the ObservableCollection.

WPF binding to a non dependency property on a control template

I'm creating a WPF custom control as an auto learning exercise. My control has a ListView inside the template. I wanto my control user be able on defining the needed columns in his own Xaml, but I did not get the strategy on how to pass the columns to the inner listview since binding with FindAncestor complain that "Columns" is not a DependencyProperty.
Wekk the questions are:
How to achieve bind a property from xaml to the template when it is not a DP
Correct my design: I think there is something wrong: if someone would change completely my template, how should I let him use the Column collection ?
why not inherit from ListView directly? Then you have all the properties you need for the ListView and can also add you own properties to the class.
Then you can apply a custom Style to your control to make it look like you want. (Here you have a basic ListView Style that you can use and expand to your needs)
Sometimes binding to a property that is not a dependency property can be solved using the Binding Mode OneWayToSource
Have you tried that?

binding checkbox in datatemplate to user control datacontext

I have a listbox which include a data template of checkbox. I need the check box data context be the user control and not the listbox datacontext. How can active that in silverlight since donot have relativesource than self or template
Thanks in advance
Silverlight does not have relative source binding "out of the box", however there is a solution here that works:
http://www.scottlogic.co.uk/blog/colin/2009/02/relativesource-binding-in-silverlight/
You can bind your CheckBox's DataContext to that of the parent User Conntrol (In this example it is called MyUserControl) as follows:
<CheckBox>
<local:BindingHelper.Binding>
<local:BindingProperties TargetProperty="DataContext"
SourceProperty="DataContext"
RelativeSourceAncestorType="MyUserCOntrol"/>
</local:BindingHelper.Binding>
</CheckBox>
If you do not want to go down this route, you could create a ViewModel that exposes a collection of objects that you bind to your checkbox, each object could provide a reference back to the parent view model.

Resources