Consider a XAML snippet like this:
<Grid>
<Button x:Name="myButton"/>
<m:MyControl Button="myButton"/>
</Grid>
Of course this doesn't work. But what I need is to pass the Button control instance named "myButton" as value to the other control's Button property. I've also tried it with {StaticResource myButton} but it fails telling me that the resource cannot be found.
What's the correct XAML syntax for that?
If MyControl.Button is a DependencyProperty you can do it so
<m:MyControl Button="{Binding ElementName=myButton}" />
Related
WPF 4 includes a "TaskbarItemInfo" Freezable class that adds an attached property to a Window that allows various Windows 7 taskbar items to be changed.
In particular, I'm trying to set progress information on the tasbar icon for the Window. I'd like to use a DataTrigger to do this, but it doesn't seem to work. I tried using a simple style setter, but that doesn't work either - only direct property assignment or direct property bindings will work.
For example:
<Window.Style>
<Style>
<Setter Property="TaskbarItemInfo.ProgressState" Value="Indeterminate" />
</Style>
</Window.Style>
<Window.TaskbarItemInfo>
<TaskbarItemInfo />
</Window.TaskbarItemInfo>
It appears as though the attached property is not being set via the style. Is my syntax for setting attached properties via styles incorrect, or am I missing something else?
TaskbarItemInfo doesn't inherit from FrameworkElement so there is no Style property for you to set in the DataTrigger.
Why don't you bind your TaskbarItemInfo's ProgressState to the property you were thinking of using in your DataTrigger and then use a ValueConverter to convert that to the relevant TaskbarItemProgressState.
<Window.TaskbarItemInfo>
<TaskbarItemInfo ProgressState="{Binding YourProperty, Mode=OneWay, Converter={StaticResource ProgressStateConverter}}" />
</Window.TaskbarItemInfo>
Then a simple converter can return whichever TaskbarItemProgressState applies to your trigger property.
If I have an explicit style defined like this:
<Style x:Key="MyButtonStyle1">...</Style>
And a Button control defined like this:
<Button x:Name="MyButton" Style="{StaticResource MyButtonStyle1}" />
Is it possible, at runtime, to extract the style name (i.e., "MyButtonStyle1") from the button control?
Note: I don't think looping through the ResourceDictionary will work because, in our app, it will have been cleared and reloaded by the time this code is run.
If you can't leverage the ResourceDictionary then no; you will need the ResourceDictionary to obtain the x:Key.
I've created a UserControl named Marked. The code for the control is :
<UserControl .... x:Name="marker">
<StackPanel Orientation="Vertical" x:Name="LayoutRoot">
<Image Source="{Binding Path=MarkSource, ElementName = marker}" Visiblity="{Binding Path=IsMarked}"/>
</StackPanel>
</UserControl>
In the code behind I've set two dependency properties: MarkSource and IsMarked.
When I use the control I do something like this:
<my:Marker MarkSource="mark.jpg" IsMarked = {Binding Path=Person.IsActive}/>
The problem is:
1. The custom control works.
2. Sometimes when I'm starting the application I receive the following error: Xaml tree error the name marker already exits.
when I restart the application it works correctly.
I've tried removing the x: from the x:Name="marker" to Name="marker" but the binding on the image doesn't work.
I've tried setting up binding in the code behind, it also doesn't work.
What is the problem with the x:Name?
Try to give x:Name to your Image control.
Remove x:Name from your UserControl.
In code behind set DataContext of your image to this.
Remove ElementName = marker in your binding.
I have a 'searchwin.xaml' user control in a Silverlight page called Mainpage.xaml. 'searchwin' has its own viewmodel 'searhwinVM'. I have set searchwin's datacontext to its viewmodel in the Mainpage xaml like this
<UserControl.Resources>
<vm:SearchWin x:Name="SearchWinVM" x:Key="SearchWinVM" /> </UserControl.Resources>
<part:SearchWin ..... {other properties here}
Visibility="{Binding Converter={StaticResource BooleanToVisibilityConverter}, Path=IsSearchVisible}"
DataContext="{StaticResource SearchWinVM}"/>
This search control I want to set as invisible by default and visible only when I change to a particular value in my listbox which is on the mainpage.xaml. Setting the datacontext of the UC in the mainpage.xaml or programatically in the mainpage.xaml.cs causes the control to always be visble. I cant get it to hide now. If I remove the datacontext it works well as expected. Any ideas how to fix this?
Thanks for your time...
Have you verified that your converter is being called at all? I'd just put a breakpoint in and see what happens.
Say I have a user control like the one below, how would I bind something to the ActualWidth of the "G1" grid from outside of the control?
<UserControl x:Class="Blah">
<WrapPanel>
<Grid x:Name="G1">
...
</Grid>
<Grid>
...
</Grid>
</WrapPanel>
</UserControl>
If you mean with outside the control, not as Content of the control, you can use ElementName in the Binding like so:
{Binding ElementName=G1, Path=ActualWidth}
If you mean outside the control in another Xaml file, then you can try to use the Path property if your control is in the scope of the other control:
{Binding ElementName=ParentControl, Path=G1.ActualWidth}
However I would advise against this design, because you may change the name of G1 one day, and you would never know of any bindings that might break.
If you want to bind to an external control where you use this user control, declare a DependencyProperty at your UserControl code behind and then Bind G1 to that property.
And bind the external control's property to the UserControl's DependencyProperty.
It is like a 2 level of indirection.