0

My knowledge of XAML relative to C# is limited, and trying to find, if possible, a more concise/efficient version of a binding/conversion expression.

... RightValue="{Binding Converter={StaticResource ListViewSingleSelectionMode}}"

The above is part of a conditional action XAML code based on Interaction Behavior for UWP. The code works as expected but my question is if the code can be confined to XAML without making a trip to a converter in code-behind.

All I wanted to do with the Converter is to return a ListView SelectionMode enumeration type: ListViewSelectionMode.Single and that is exactly what the IValueConverter based code-behind does with a single-liner return as:

return ListViewSelectionMode.Single;

Yes, you may have guessed it, the missing part with LeftValue= is bound to a ListView Control, and the RightValue shown above is to compare if the ListView SelectionMode is equal to ListViewSelectionMode.Single to implement a conditional action. Note that x:Static doesn't work for UWP as indicated under comments below.

Can "{Binding Converter={StaticResource ListViewSingleSelectionMode}}" equivalently be represented in XAML without making a round-trip to the code-behind converter (noting that it should be an enum ListViewSelectionMode.Single)?

user2921851
  • 990
  • 12
  • 26
  • It's not clear why you are using the converter in the first place. What are you converting? What's the source? If none, do you really just want `RightValue={x:Static ListViewSelectionMode.Single}`? – Peter Duniho May 04 '17 at 00:14
  • So, may be the answer then. I totally forgot about x:Static (but does this work for WinRT?) and my mind was somehow geared to Binding and Converter for some reason. Will check and see if it works. – user2921851 May 04 '17 at 10:38
  • Update: No, x:Static doesn't work for UWP. I will make my question more explicit in terms of UWP. – user2921851 May 04 '17 at 10:45
  • In most cases, simply setting the property value by enum value name directly should work. I.e. `RightValue="Single"`. I think usually, this will require that `RightValue` be declared to be the enum type desired. In scenarios where that doesn't work, you can use the element syntax, as in the marked duplicate. – Peter Duniho May 04 '17 at 15:39
  • Setting property value by value name doesn't work. Throws an exception as it sees as a string value which makes sense without any converter. The best I could come up is **x:Bind** to a read-only property in view-model which is better than my initial random thought based on a converter. The improved version is: `RightValue="{x:Bind ViewModel.ListViewSelectionModeSingle, Mode=OneWay}"` and `ListViewSelectionModeSingle` is simply a property that is `ListViewSelectionMode.Single` enum getter. Works fine. If someone can do this exclusively in XAML, please let me know. – user2921851 May 04 '17 at 22:04
  • _"Throws an exception as it sees as a string value which makes sense without any converter"_ -- makes sense when the XAML compiler doesn't know what type to expect. In other scenarios, where the target property is strongly-typed, it should work fine (so I assume in your example `RightValue` is not strongly-typed...impossible to know, since you don't provide a [mcve]). _"If someone can do this exclusively in XAML, please let me know"_ -- did you look at the marked duplicate? That should work fine. – Peter Duniho May 05 '17 at 01:49

0 Answers0