Setting binding of subproperty - silverlight

I have an element on the my usercontrol
<myControls:MonitorWindow x:Name="WindowFrame" MinHeight="400" />
and I need to change its subproperty
MonitorWindow.CloseButton.Visibility.
Is it possible bind property from ViewModel to that property?
One way is ElementBinding with TwoWay Mode to fake field, but it isn't very nice.

You should Make a Dependency Property of the Type Visibility so that its can be binded to your control and can be set from outside the control.
For Ex Let the Dependency Property is named as CloseButtonVisibility is present in codebehaind of your user Control (use propdp then press TAB or double TAB as snippet shortcut to create one)
IN Your Control
<UserControl Name="Control">
<Button Visibility="{Binding Path=CloseButtonVisibility,ElementName=Control,Mode=TwoWay}"></Button>
</UserControl>
While using your Control
<myControls:MonitorWindow x:Name="WindowFrame" MinHeight="400" CloseButtonVisibility="Hidden" />
This might help... :)

You should do two things:
First you should expose the CloseButtonVisibility as a property.
The second thing is, in order to bind value to this property you should define this property as Dependcy property, see the following link for explanation on how to define dependency property:
UNDERSTANDING: DEPENDENCY PROPERTIES IN SILVERLIGHT
Good Luck,
Lior

Related

Cannot bind to additonal property in extended Control

I am reasonably proficient in XAML and WPF having trouble with binding to an additional control within an extended control from outside the control. Sorry, that's a real mouthful so let me explain:
I have a control that I have extended from a ComboBox and applied the template and overridden the property metadata and all that stuff and re-templated it so it looks and works as I want it to. Now, I want to add a TextBox to provide search functionality for the ComboBox which I have exposed dependency properties to determine if it is visible or not and added this to the first row of the Grid above the ItemsControl and all works fine. I have added a dependency property called IsFiltered and applied a template binding to determine if the filter is visible and from outside my control I can set this value and it all works.
However, I have added a dependency property to the extended ComboBox (MyComboBox if you like) as a string property so that I can assign a filter text property from my view model that will eventually work its way to the text box embedded within the control. The TextBox in the conrol is also bound using {TemplateBinding FilterText} dependency property, as it hooks back to my MyConboBox control and the assignment is accepted and recognised. However, while the property from my view model is set and read and interacts with the FilteText property in MyComboBox to which the TextBox inside by MyComboBox control template is also bound to, the TextBox does not trigger a change.
<TextBox
Grid.Row="0"
Margin="4"
Text="{TemplateBinding FilterText}"
BorderBrush="Red"
Visibility="{TemplateBinding IsFiltered, Converter={converters:BoolToVisibilityConverter}}"/>
Can anyone help?

Binding UI-element to UI-element like Textbox to Textbox

I have UI-elements like a textbox and I want to bind them to a ViewModel. I need to access many properties of the textbox like the text- or the IsEnabled-property.
Is it possible that I bind the textbox directly to another textbox in the ViewModel with all their properties instead of binding every single property to properties?
Yes, using an ElementName, but you still bind all properties though.
<!-- Bound to ViewModel -->
<TextBox Name="tbOne" IsEnabled="{Binding OneIsEnabled}" Text={Binding TextOne}/>
<TextBox Name="tbTwo" IsEnabled="{Binding ElementName=tbOne, Path=IsEnabled}" Text={Binding ElementName=tbOne, Path=Text}/>
There is no built-in way to bind all dependency properties of the TextBox to another. Personally, I would prefer binding directly to the ViewModel.
An alternative solution would be to create a UserControl that internally clones the TextBox with all bindings:
<CloneControl Target="{Binding ElementName=tbOne}"/>
Here, the ControlControl would inspect the target, and have code that create a new TextBox, and sets the bindings in code. This is only useful if you are doing this very often, and there is a slightly performance price to pay, as you are adding another level of controls to the UI tree.

WPF bind visibility of element to another element with attached property of specifc value

I have the following xaml code:
<Window.Resources>
<BooleanToVisibilityConverter x:Key="b2v" />
</Window.Resources>
I would like to bind the visibility of a RichTextBox to the check state of a CheckBox.
<RichTextBox Visibility="{Binding IsChecked,ElementName=chk,Converter={StaticResource b2v}}" />
No problem so far. But the problem is that my CheckBox has no Name property (I can't give it a name because it is copied over time).
Due to that I have an attached property for the CheckBox
<CheckBox local:MyUIElementAttributes.AttachedElementType="TitleCheckbox"/>
Now I would like to use this attached property to identify the CheckBox that the visibility of my RichTextBox is to be bound to.
How can I do that? I have to bind to the attached property and have to check the value of that to set the visibility.
Thank you!
If I understand you correctly, you want to use the value of your Attached Property as the value of the Binding.ElementName property. From the Binding.ElementName Property page at MSDN:
Property Value
The value of the Name property or x:Name Directive of the element of interest. You can refer to elements in code only if they are registered to the appropriate NameScope through RegisterName. For more information, see WPF XAML Namescopes.
Having read this and the linked page, it seems as though you can use the NameScope.RegisterName method instead of setting the Name property. You should then be able to refer to the name set with this method in the Binding.ElementName property without issue.
Disclaimer: I have not tried this before and could be mistaken.

Get reference to Xaml object in view model

I have a an object created in Xaml:
<Grid>
<MyObject/>
</Grid>
I need someway to bind the object myObject back to a property in my view model. I dont know whether this is possible, everything ive seen so far binds properties together, but any help would be greatly appreciated.
I am assuming what you want is your ViewModel to hold the actual visual control MyObject in it and your Grid to display it via MVVM.
This is possible through ContentControl in WPF.
Assuming your ViewModel has a property MyObjectView which holds MyObject...
<Grid>
<ContentControl Content="{Binding MyObjectView}" />
</Grid>
Having said that you must take caution that same MyObjectView is not bound to any other content control as that will result in an error
"Specified element is already the logical child of another element.
Disconnect it first"
And if that requirement is possible then you must excercise ContentTemplate option.
Let me know if this helps.
It is possible. It kinda breaks mvvm though.
You can attach an InvokeCommandAction to this object, and bind the CommandParameter to it via ElementBinding. Then in the callback of the command which you defined in the viewmodel, you will have a reference to this object from the CommandParameter.

Setting a property on the ViewModel from the View in WPF

I have a dependency property on my ViewModel which is the DataContext for my View. The ViewModel has no reference to the View. The property on the ViewModel is going to reference a control on the view, but I need to be able to set this property in XAML.
How is this possible? One thought I had was to develop a custom control which has a Property property and a Value property, so you could do something like this in the View to set the property on the ViewModel:
<PropertySetter Property="{Binding MyViewModelDependencyProperty}" Value="{Binding ElementName=aControlOnMyView" />
Before I went down this route, I wanted to check if there was any other approach I could take?
Thanks for the detailed reply Ray, but if I give you a bit more detail about the problem I'm trying to solve, you might get a better idea of why I mentioned the approach I did.
Basically, what I'm trying to do is set the focus to a textbox when the user hits a button. I've written an attached property which you can attach to the Button control, specify what the trigger event is (in this case the 'Click' event), and then what control to focus on. This works really nicely, and keeps everything in XAML.
However, I now have a use case where the focus should be set to an arbitrary text box from the click event on a button which is part of a toolbar. This toolbar is itself a user control which is sitting inside another user control, which is inside another user control! This toolbar needs to be reusable across various different forms, and each time, the control to set focus on after you click the button will be different per form.
That's why I had the idea of making the focus control (i.e. a textbox) a property on the view model itself (on my ViewModel base to be precise), and have the ViewModel base code (which the toolbar is bound to), set the focus to the control when the button is clicked (and the e.g. Add/Edit method is called on the ViewModel base).
In unit test land, the control to focus on property will be null, so it's .Focus() method just won't be called. So I can't see an issue there. My problem is then how you set the focus control property from XAML, which is why I had the PropertySetter idea.
I don't like the fact that the ViewModel has any reference to controls sitting on the view, but I can't see another way to achieve what I need. What if the logic that dictates whether to set focus to the control is quite complex? This would sit in the ViewModel surely? Therefore, is there any harm in the ViewModel having this UIElement property? It still knows nothing about the specific View it is bound to, it just knows that there is a control which it needs to set focus to when some action happens on the ViewModel.
My first reaction (and it's a strong one) is so say "Don't do that!" By giving your view model a reference to a part of your UI you are breaking the encapsulation that makes view models so powerful and useful.
For example, what if you want to unit test your view model or serialize it to disk? In each case the piece of your UI will not be present, because there will be no view at all. Your tests will miss coverage and your reconstitution will be incomplete.
If your view model actually needs references to UI objects and there is no better way to architect it, the best solution is to have the view model itself construct those controls it requires a reference to. Then your view can incorporate that control as the Content of a ContentPresenter via binding and provide a Style to configure the control, including a ControlTemplate to provide its content. Thusly:
public class MyViewModel
{
public ListBox SpecialControl { get; set; }
public MyViewModel()
{
SpecialControl = new ListBox();
}
}
and
<DataTemplate TargetType="{x:Type local:MyViewModel}">
<DataTemplate.Resources>
<Style TargetType="ListBox" ... />
</DataTemplate.Resources>
...
<ContentPresenter Content="{Binding SpecialControl}" />
</DataTemplate>
Other possibilities are:
Have the view model actually derive from the Control class, then override OnApplyTemplate() and use GetTemplateChild to find a template item whose name starts with "PART_"
Implement an attached property that takes a property name, finds that property in the DataContext, and sets it to the DependencyObject to which the property is attached.
Implement your PropertySetter idea
My option #2 would look like this:
<DataTemplate TargetType="{x:Type MyViewModel}">
...
<TextBox local:PropertyHelper.SetViewModelToThis="SpecialControl" />
...
</DataTemplate>
The code in the SetViewModelToThis PropertyChangedCallback would get the view model from the DataContext, reflect on it to find the "SpecialControl" property, then set it to the TextBox. Note that the implementation of SetViewModelToThis must take into account the possiblity that DataContext is not set right away, and that it maybe changed requiring the old setting to be removed and a new one made.
First of all, the DataContext of the control should be the ViewModel object and not a property of it. Second, when you TwoWay bind a property of ViewModel to your control, changes in the control's value will update (in your case, 'set') the value of ViewModel's property.

Resources