Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# replace Binding with x:Bind (Migrate UWP to WinUI 3)

Tags:

c#

winui-3

xbind

I have a Page.Resources Datatemplate, there I specify an x:DataType

     <Page.Resources>
         <DataTemplate x:Key="DataPanelTemplate" x:DataType="dataModel:Activity">
    

the problem is in this DataTemplate I have this AppBarButton:

      <AppBarButton Icon="Stop" IsCompact="True" x:Name="cmdStopActivity" >
                    <Interactivity:Interaction.Behaviors>
                        <Core:EventTriggerBehavior EventName="Click">
                            <Core:InvokeCommandAction Command="{Binding ActivitiesViewModel.FinishActivityCommand, Source={StaticResource Locator}}" 
                                                      CommandParameter="{Binding}"/>
                        </Core:EventTriggerBehavior>
                    </Interactivity:Interaction.Behaviors>
                </AppBarButton>
  

How can I define the InvokeCommandAction Command as x:Bind? There I need access to the ViewModel and not to the specified x:DataType. I tryed this:

     xmlns:viewModel="using:PnxZeiterfassung.ViewModel"
     x:Bind viewModel:ActivitiesViewModel.FinishActivityCommand
      public RelayCommand<Activity> FinishActivityCommand { get; set; }
      FinishActivityCommand = new RelayCommand<Activity>(async (a) => await FinishActivity(a));

but here the FinishActivityCommand is not recognized.

like image 539
KarsaOrlong1981 Avatar asked Jun 04 '26 21:06

KarsaOrlong1981


1 Answers

In a DataTemplate, you can only use x:Bind to bind to a property of the the type specified by the x:DataType attribute.

You cannot x:Bind to a view model property from inside a DataTemplate so, in this case, it's not possible or supported to use x:Bind to bind the CommandParameter property to a property of the view model. You should stick with the {Binding} markup.

like image 159
mm8 Avatar answered Jun 07 '26 11:06

mm8



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!