binding usercontrol - c#

How can I create a binding to a property defined in an UserControl from XAML which includes it? For example, I have a control of type local:Photo named thePhoto, which has three controls local:Layer called Main, Frame and Text, and I want to access thePhoto.Main.ActualWidth from my MainWindow? Thanks
(I forgot to say that simple Binding with Path and ElementName doesn't work)

Create DependencyProperty of desired type within "parent" UserControl and bind it with needed Properties on both sides.
Nice example of doing stuff like that:
Exploring the use of Dependency Properties in User Controls

Related

How can i modify XAML elements from another class (C#-WPF)

I am trying to connect a class called Engine with XAML elements, to be more specific I have a LevelWindow.xaml where a grid exists. I want to split that grid (in rows and columns ) from Engine class, the split code exists, but when the programs is running Engine is not changing grid from LevelWindow .... I tryed to semi-use bindings, but i cant find any way to Bind the GRID .... some sugestions ?
Engine
If you absolutely want to use your existing splitting code and that it doesn't support binding and/or notify property changed, you can perhaps find some way to perform what you want using a ContentControl instead of your Grid in your xaml file.
Then Bind the contentControl content property to a grid that you instantiate in your Engine (thus you can access and modify it's inner properties like grid column/rows).
Yet it's quite horrible to perform graphic control instantiation in your business class...
Link to MSDN contentcontrol :
https://msdn.microsoft.com/fr-fr/library/system.windows.controls.contentcontrol(v=vs.110).aspx

Creating User Controls without predefined properties C# WPF

I am aiming at creating different UserControls for my college project, in which I am attempting to use ContentControl to wrap my UserControl. I've placed other Controls like Image, WebBrowser, MediaElement and the like, now I have reached a stage where I need to set the properties for my UserControls. Thus, I thought of making use of PropertyGrid Control, but now the problem I am facing is in the PropertyGrid control, as I get all the default properties of the Controls, which in my case I don't want.
For Eg: if I use Image Control then i need properties like Source and Stretch to be displayed only in the PropertyGrid. Can anyone help me in achieving this?
I tried to override some default properties like "Name" and assign it as [Browsable(false)] to hide it from being displayed. I don't want to do this for all the other properties which are being displayed and which are not under my requirements as well.
I am using Xceed.Wpf.Toolkit for my PropertyGrid.
It is explained in the documentation on how to do that:
http://wpftoolkit.codeplex.com/wikipage?title=PropertyGrid
When you change the selected object see SelectedObjectType and set PropertyDefinitions in code to match the properties you want to see for that type of object.

Multiple pages in a single window C# WPF

Is there a control that allows to choose data, that should be in it? For example: red one - static page, blue is dynamic, below are buttons that allow to switch pages in dynamic part.
I use the dockpanel suite for this:
dockpanel suite
This contol is designed to mimic the vs.net tabs.
Try using a ContentControl databound to a property on your UserControl. Change the property when you want to update the content. Ensure that the property is either and DependencyProperty or that your user control implements INotifyPropertyChanged.
http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.aspx
This is achived by using DataTemplates. You just bind the SelectedItem of the "red" control (e.g. a ListView) to the Content of the "blue" Control (it should be a ContentPresenter).
You add DataTemplates to the Resources accessable by the "blue" control which will then display different content depending on the underlaying class/data.

Abstract user control

I am fairly new to WPF MVVM.
I have a user control named MyButtonsView which inherits from UserControl. This user control should display two other user controls.
I have few viewmodel classes:
MyRedButtonViewModel, MyBlueButtonViewModel.
They both derive from an abstract class named MyAbsButtonViewModel. This abstract view model enforces all derive view models to implement a property name GetImage.
In the XAML of MyButtonsView, I have a grid that should have two rows. In the first row I want to have MyRedButtonViewModel, in the second row I want to have MyBlueButtonViewModel. Both should display an image inside a button which is binded to GetImage property. (It is polymorphic).
How can I tell the XAML to load the right viewmodel for each row?
is there a proper way to implement it. I am mind opened to change my design.
You can define use different ViewModel as DataContext for the two buttons.
MyButtonsView binding to MyButtonsViewModel, MyButtonsViewModel has two properties MyBlueButtonViewModel and MyRedButtonViewModel.
In the MyButtonsView you can define somethings like this:
<MyRedButton DataContext = "MyRedButtonViewModel" ..../>
<MyBlueButton DataContext = "MyBlueButtonViewModel" .../>

Instantiate ItemsPanelTemplate

I'm trying to create a ItemsControl that has a separator between items, for example a control to create a navigation bread crumb. I want the control to be completely generic.
My original method was to create extend ItemsControl, add a SeparatorTemplate property, and then have the class add separators to the ItemsHost of the ItemsControl. The problem with this approach is that if you add extra items to the container panel, the ItemGenerator gets confused and the items are out of order and don't get removed correctly.
So my second plan was to create a completely new control that would emulate an ItemsControl, but the problem I'm running into is that I can't find a way to instantiate an ItemsPanelTemplate. I would like to provide an ItemsPanel property just like ItemsControl, but I can't then create a panel from that template.
Can anyone think of a way to either instantiate an ItemsPanelTemplate or way to add controls to an ItemsControl's panel without breaking the ItemGenerator?
Well I haven't tried it myself but I would have thought you would override the GetContainerForItemOverride to acheive this.
You could create a new BreadCrumbItem control which is a templated ContentControl that has in its default template the typical ContentPresenter and whatever you want to use by default as separator all in a Grid or StackPanel.
The GetContainerForItemOverride generates a new instance of this BreadCrumbItem sets its ContentTemplate to the ItemTemplate property from your ItemsControl derivative (the BreadCrumb control?).
Your BreadCrumb control would also expose BreadCrumbItemStyle property that you assign to the BreadCrumbItem you create during GetContainerForItemOverride.
For completeness you may need to also implement the other *Container*Override methods in your BreadCrumb control.

Categories