Wpf bind string to the name property - wpf

I have a button in a dynamically loaded XAML file.
<Button Name="{Binding Template_Text1}"
Width="800" Height="76"
Content="{Binding Template_Text1}"
Style="{DynamicResource RoundedButton}"/>
If I give a static NAME to the button everything goes ok.
so, how can I bind a string variable to the NAME property?

From the MSDN:
Name is one of the very few dependency properties that cannot be
animated ( IsAnimationProhibited is true in metadata), because the
name itself is vital for targeting an animation. Data binding a Name
is technically possible, but is an extremely uncommon scenario because
a data-bound Name cannot serve the main intended purpose of the
property: to provide an identifier connection point for code-behind.
In other words: what you are doing is very tricky so think again whether or not you really need it.

Keeping #Erno's valuable input in mind above, I think you can change Name of a property via binding through an attached behavior.
<Button local:NameAnimationBehavior.Name="{Binding Template_Text1}" ... />
And inside the NameAnimationBehavior, in NameProperty's dependency property changed handler, change the sender's Name property with e.NewValue.

Related

Setting binding of subproperty

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

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.

is there anyway to get Hyperlink content from resource file in WPF

Is there anyway to get hyperlink content from resource file in xaml. I know there is way on back hand code. I have tried using Name="{x:Static Properties:Resources.stringname}" but it throws error
Or if there is any work around?
I have tried Name but it gives an error:Not able to bind to Uid or Name property in XAML
How about:
<Hyperlink NavigateUri="{x:Static Properties:Resources.SomeUrl}">
<Run Text="{x:Static Properties:Resources.SomeUrl_Description}"/>
</Hyperlink>
You cannot bind to the Name in XAML since it is being used internally to create references and it needs to be unique. Some relevant bits from MSDN:
Name is one of the very few dependency properties that cannot be animated (IsAnimationProhibited is true in metadata), because the name itself is vital for targeting an animation. Data binding a Name is technically possible, but is an extremely uncommon scenario because a data-bound Name cannot serve the main intended purpose of the property: to provide an identifier connection point for code-behind.
Names must be unique within a namescope. For more information, see WPF XAML Namescopes.

Databinding to functions in dynamically-loaded plugins

I have several MenuItems whose Commands are bound to my ViewModel. Until today, all of them execute properly.
Now I have added a MenuItem whose ItemsSource is bound to an ObservableCollection. The point of this MenuItem is to enumerate a list of plugins so that the name of all plugins show up. Then when the user clicks on a plugin name, it should call a function to display properties for audio filters.
In my current implementation, which doesn't work, I tried to databind like this:
<MenuItem Header="Filters" ItemsSource="{Binding FilterPluginNames}">
<MenuItem.ItemContainerStyle>
<Style>
<Setter Property="MenuItem.Command" Value="{Binding ShowFilterDialogCommand}" />
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
The problem is that I get a BindingExpression path error because it's trying to use a String as the MenuItem's DataContext.
This leads me to believe that the DataContext for a MenuItem's MenuItems is automatically set to type of objects in the ItemsSource. Is this true?
If I need to change the DataContext, then I'd like to change it to the ViewModel that handles all of my other Commands. But if I do that, how in the world am I able to tell which plugin I want to display filter properties for? I'd need to pass in a CommandParameter at the very least, but binding this value to the filter name isn't my most favorite option. Are there any other ways to do this?
If the DataContext is indeed automatically set to the object type in the ObservableCollection, then I'd rather just call my interface method ShowFilterProperties() directly. I bet that I can't do this without Command binding. If that is the case, how do you all deal with this sort of application? Do you make all of the plugins expose a command handler, which will then show the dialog?
EDIT -- I modified my code to change the ObservableCollection type, and sure enough, WPF wants to databind to the type T. So I guess one option is to have the plugin expose the ICommand, but I don't know if this is a weird approach or not?
EDIT -- ok, I just learned something new. Interfaces can't have fields, so is it not possible to databind with plugins, period?
You are likely not quite binding like you think you are. You might want to just put some diagnostics on your bindings and see what object they are binding to. Here is a good link for debugging bindings:
http://www.beacosta.com/blog/?p=52
Here's a sample:
<Window …
xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
/>
<TextBlock Text="{Binding Path=Caption, diagnostics:PresentationTraceSources.TraceLevel=High}" … />
I think your approach is correct... it probably just needs to be debugged a little.

How does a XAML definition get turned into an object instance?

XAML allows you to specify an attribute value using a string that contains curly braces. Here is an example that creates a Binding instance and assigns it to the Text property of the TextBox element.
<TextBox Text="{Binding ElementName=Foo, Path=Bar}"/>
I want to extend XAML so that the developer could enter this as valid...
<TextBox Text="{MyCustomObject Field1=Foo, Field2=Bar}"/>
This would create an instance of my class and set the Field1/Field2 properties as appropriate. Is this possible? If so how do you do it?
If this is possible I have a followup question. Can I take a string "{Binding ElementName=Foo, Path=Bar}" and ask the framework to process it and return the Binding instance it specified? This must be done somewhere already to make the above XAML work and so there must be a way to ask for the same thing to be processed.
The Binding class is a Markup Extension. You can write your own by deriving from System.Windows.Markup.MarkupExtension.
ElementName and Path are simply properties on the Binding object.
As for the followup you can create a new Binding in code by instantiating the Binding object. I do not know of a way to process a string through.
take a look at markupextensions
http://blogs.msdn.com/wpfsdk/archive/2007/03/22/blogpost-text-creatingasimplecustommarkupextension.aspx

Resources