Is there way to get name of property in XAML?
I found there is no support for nameof in XAML.
Something serving like this:
<i:InvokeCommandAction
Command="{Binding
Source={StaticResource SomeViewModel},
Path=SomeICommandImplementation}"
CommandParameter={Binding
Source={StaticResource SomeViewModel},
Path=SomeProperty,
GetNameOf=True}" />
Is there way to get string name of property in XAML.
No, there isn't. XAML is a markup language and it has no nameof operator defined.
What you could do is to try to implement your own custom InvokeCommandAction.
Create a class that derives from System.Windows.Interactivity.TriggerAction<DependencyObject>, add the properties of InvokeCommandAction (it is sealed so you cannot derive from it) and another GetNameOf property to it and then override the Invoke method to use the nameof operator.
Related
I have a WPF application using MVVM pattern, where I have many ObservableCollections. Instead of putting these ObservableCollections into each ViewModel, I placed them into a static class called Observables, which is a member of static class AppCommon. So I can access all observable collections through AppCommon.Observables.AnyObservableINeed.
Now I need to change bindings of UserControl's so they bind to these global ObservableCollection's but I don't know how to refer to these ObservableCollections without changind the DataContext.
I tried adding namespace like
xmlns:globals="clr-namespace:Demirbaş.Globals"
and then in the ListBox setting the ItemsSource property like
<ListBox ItemsSource="{Binding Source={globals:Observables.TaşınırSınıfları}}"
but that would give me following error:
'{globals:Observables.TaşınırSınıfları}' value is not a valid MarkupExtension expression. Cannot resolve 'Observables.TaşınırSınıfları' in namespace 'clr-namespace:Demirbaş.Globals'. 'Observables.TaşınırSınıfları' must be a subclass of MarkupExtension.
What is the problem here? Am I using the right XAML syntax to bind to these collections?
EDIT
ItemsSource="{Binding Source={x:Static globals:AppCommon.Observables.TaşınırSınıfları}}" gives me error :
Cannot find the type 'AppCommon.Observables'. Note that type names are case sensitive.
I think it cannot refer to nested classes, is it right? What's the solution?
Thanks
I don't know the namespace of your application but try
xmlns:local="clr-namespace:Demirbaş"
<ListBox ItemsSource="{Binding
Source={x:Static local:AppCommon+Observables.TaşınırSınıfları}}" />
You need to use the x:Static markup extension like LPL suggested in a comment to tell WPF it's a static object
<ListBox ItemsSource="{Binding
Source={x:Static globals:Observables.TaşınırSınıfları}}" />
This error can also occur when the namespace reference is not fully qualified and the target binding exists in another assembly.
For example, xmlns:l="clr-namespace:AssemblyA.Namespace;assembly=AssemblyA".
If the specific assembly is not specified, the same error message will be displayed "value is not a valid MarkupExtension expression".
I would like to put my RowValidationRules class as a resource and then reference the Key on the datagrid but I'm not 100% sure on how to get there.
<Window.Resources><helper:AccountRoleValidationRule x:Key="MyAccountRoleValidator" /></Window.Resources>
<DataGrid.RowValidationRules><helper:AccountRoleValidationRule ValidationStep="UpdatedValue" /></DataGrid.RowValidationRules>
I would like to do something like <DataGrid RowValidationRules="{StaticResource MyAccountRoleValidator}" /> but I get 'RowValidationRules' property is read-only and cannot be set from markup.
In the end I'm going to use FindResource("MyAccountRoleValidator") from my .xaml.vb file to check the validation result on my CanSave() ICommand.
Try element syntax:
<DataGrid.RowValidationRules>
<StaticResource ResourceKey="MyAccountRowValidator"/>
</DataGrid.RowValidationRules>
(StaticResource will not show up in the not so intelligent IntelliSense in VS, but when it is written out the property will)
HI
Am load a string xaml with DynamicResource assigned to a Background property. Is there a way to get the reference of the dynamic resource.
Background="{DynamicResource Color1}"
I want to get the resource reference assigned to a Dependency property at runtime
Pl help
Use FrameworkElement.FindResource Method
this.FindResource("Color1");
Where is the DependencyProperty defined? On the same Window/UserControl? If you simply want to bind to the value of a DependencyProperty you probably want to use regular {Binding ...} syntax instead.
Example 1: If you are binding to a dependency property on a particular control named myControl you can declare it like below.
Background="{Binding ElementName=myControl, Path=Color1}"
Example 2: If you don't want to rely on naming controls because it is so pasay in WPF and you are referencing a property defined on your Window you could do something like below.
Background="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Color1}"
I have a DataTemplate that I'm using as the CellTemplate for a GridViewColumn.
I want to write something like this for the DataTemplate:
<DataTemplate
x:Key="_myTemplate">
<TextBlock
Text="{Binding Path={Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GridViewColumn}}, Path=Header}}" />
</DataTemplate>
My GridView is bound to a DataTable, and I want to bind to the column of the DataTable whose name is equal to the Header of the GridViewColumn the DataTemplate is attached to. [I hope that made sense!]
Unfortunately, this doesn't work. I get a XamlParseException: "A 'Binding' cannot be set on the 'Path' property of type 'Binding'. A 'Binding' can only be set on a DependencyProperty of a DependenceyObject."
How can I set this up?
Edit (elevating comment by DanM to the question)
I basically need a DataTemplate whose binding is determined by the DataContext and which column the DataTemplate is attached to. Is there an alternative?
You cannot assign a Binding to just any property. The property must either of the type Binding or be implemented as a Dependency Property on the object.
The Path property of the Binding class is of type PropertyPath and Binding does not implement the Path property as a dependency property. Hence you cannot dynamically bind the Path in the way you are attempting to.
Edit
You basically want to embed metadata in your bound data which drives the configuration of the DataTemplate. This can't be done in XAML alone. You would need at least some support from code.
It would seem to me that the best approach would be to use a ViewModel. That makes the binding in the XAML straight-forward and pushes this "what to bind with what" decision down into the code of the ViewModel.
Don't you just want this?
{Binding Path=Header, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GridViewColumn}}}
I understand that Silverlight 3.0 has binding but just want a simple example on how to use this to read a property from a class.
I have a class called Appointment which as a String property called Location:
Public Property Location() As String
Get
Return _Location
End Get
Set(ByVal Value As String)
_Location = Value
End Set
End Property
With a Private Declaration for the _Location as String of course.
I want a XAML element to bind to this property to display this in a TextElement, but it must be in XAML and not code, for example I want something like this:
<TextBlock Text="{Binding Appointment.Location}"/>
What do I need to do to get this to work?
It has to be a Silverlight 3.0 solution as some WPF features are not present such as DynamicResource which is what I'm used to using.
Just to add that my XAML is being loaded in from a seperate XAML File, this may be a factor in why the binding examples don't seem to work, as there are different XAML files the same Appointment.Location data needs to be applied.
You have two options.
If the "Appointment" class can be used as the DataContext for the control or Window, you can do:
<TextBlock Text="{Binding Location}" />
If, however, "Appointment" is a property of your current DataContext, you need a more complex path for the binding:
<TextBlock Text="{Binding Path=Appointment.Location}" />
Full details are documented in MSDN under the Binding Declarations page. If neither of these are working, make sure you have the DataContext set correctly.
You need something in code, unless you want to declare an instance of Appointment in a resource and bind to that but I doubt thats what you want.
You need to bind the Text property to the Property Path "Location" then assign the DataContext of the containing XAML to an instance of the Appointment:-
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Text="{Binding Location}" />
</Grid>
Then in the control's load event:-
void Page_Loaded(object sender, RoutedEventArgs e)
{
this.DataContext = new Appointment() { Location = "SomePlace" };
}
Note in this case I'm using the default Page control.
If I'm reading correctly, you need to create an instance of Appointment, set the DataContext of the control to that instance and modify your binding to just say: Text="{Binding Location}"
Also, consider implementing INotifyPropertyChanged on your Appointment class to allow the data classes to notify the UI of property value changes.