I'm trying to use MVVMLight to bind a TreeViewItem Selected event to a command.
The TreeViewItem's are defined in a HierarchicalDataTemplate so I cannot add Interaction.Triggers (as shown below)
<HierarchicalDataTemplate
x:Key="TreeViewItemTemplate"
ItemsSource="{Binding ChildReportViewModels}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Selected">
<MvvmLight_Command:EventToCommand Command="{Binding LoadReportCommand, Mode=OneWay}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</HierarchicalDataTemplate>
How else can I add the EventTrigger to each TreeViewItem?
Thanks.
I forgot about this question.
For future ref, here's the solution I used...
Instead of trying to bind the EventToCommand to the Selected event of the TreeView,
I bound it to the MouseLeftButtonUpEvent of the TextBlock declared in the HierarchicalDataTemplate for TreeViewItems.
<HierarchicalDataTemplate
x:Key="TreeViewItemTemplate"
ItemsSource="{Binding ChildReportViewModels}"
ItemContainerStyle="{StaticResource TreeViewItemContainerStyle}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<gs_cmd:EventToCommand Command="{Binding LoadPublicationCommand, Mode=OneWay}" CommandParameter="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
I have not much knowledge about MVVMLight and especially about EventTrigger.
But since there is no answer to your qestion yet the codeplex article TreeViewWithViewModel might help. It shows how to bind to SelectedItem and IsExpanded Properties in a wpf-treeview and how these can be used to implement load on demand in the treeview.
Related
I want to change the Source of an ImageButton in xaml based on the Theme (i.e. Light vs Dark). I get a System.NullReferenceException 'Object reference not set to an instance of an object.' when I use the following:
<ImageButton
Grid.Row="1"
Margin="10"
Command="{Binding CreateCommand}"
Source="{AppThemeBinding Light=add_box_black_48dp.svg, Dark=add_box_light_48dp.svg}"
HorizontalOptions="End"
VerticalOptions="End"/>
If I change the Source to the following everything works
Source="add_box_white_48dp"
Can I use AppThemeBinding to change the ImageButton Source in this way?
<Edited on 6/4/2022 to show xaml for cases that work and those that do not work. Also changed the names of the svg files to reflect light and dark cases>
I am using Visual Studio Community 2022 (64-bit) - Preview
Version 17.3.0 Preview 1.1
This ImageButton xaml throws the exception:
<ImageButton
Margin="10"
Command="{Binding CreateNewAccountCommand}"
Source="{AppThemeBinding Light=add_light.svg, Dark=add_dark.svg}"
HorizontalOptions="End"
VerticalOptions="End"
BackgroundColor="#376489"
CornerRadius="8"
WidthRequest="36"
HeightRequest="36">
</ImageButton>
This ImageButton xaml works and does not throw an exception
<ImageButton
Margin="10"
Command="{Binding CreateNewAccountCommand}"
Source="add_light.svg"
HorizontalOptions="End"
VerticalOptions="End"
BackgroundColor="#abdbe3"
CornerRadius="8"
WidthRequest="36"
HeightRequest="36">
</ImageButton>
This also works and does not throw an exception
<ImageButton
Margin="10"
Command="{Binding CreateNewAccountCommand}"
Source="add_dark.svg"
HorizontalOptions="End"
VerticalOptions="End"
BackgroundColor="#376489"
CornerRadius="8"
WidthRequest="36"
HeightRequest="36">
</ImageButton>
To work cross-platform, refer to .png files. These get built automatically by Maui:
Each image resource needs Property/BuildAction: "MauiImage".
Refer to .png in xaml:
Source="{AppThemeBinding Light=add_box_black_48dp.png, Dark=add_box_light_48dp.png}"
Verified by modifying Maui project's default MainPage, to say:
<Image Source="{AppThemeBinding Light=dotnet_bot.png, Dark=dotnet_bot.png}" ... />
This refers to a Media item dotnet_bot.svg, which (I infer) gets converted by "MauiImage" into a .png resource.
NOTE: Maybe the plan is to be able to leave off the extension. This works on Android, but the image does not show on Windows:
<!-- Doesn't work currently on Windows -->
<Image Source="{AppThemeBinding Light=dotnet_bot, Dark=dotnet_bot}" ... />
I'm trying to do a DropDownButton (or ComboButton) in Pure XAML because I want to use the GUI in PowerShell.
I'm aware of wpftoolkit's DropDownButton but it needs custom C#/c++ code to make it work.(and if I could bring it in, the wpftoolkit would be bigger than my project!)
Below I just stacked a Button control on top of a comboBox control. (Ugly, I know but my options are limited from my point of view)
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="580"
MinHeight="580"
MinWidth="700"
Width="700"
Background="lightgray">
<Grid>
<ComboBox
x:Name="CboxWakeUp"
Width="100"
Height="80"
HorizontalAlignment="Right"
Margin="10,10,10,0"
VerticalAlignment="Top"
Background="green"
IsEditable="True"
IsReadOnly="True"
TextBlock.FontSize="16"
>
<ComboBoxItem Content="WakeUp and RDP" IsSelected="True"/>
<ComboBoxItem Content="WakeUp from list"/>
<ComboBoxItem Content="WakeUp and Run"/>
</ComboBox>
<!-- Button is ON TOP of ComboBox -->
<Button
Name="btnWakeUp"
Width="85"
Height="80"
HorizontalAlignment="Right"
Margin="10,10,25,0"
VerticalAlignment="Top"
Background="green"
TextBlock.FontSize="16"
>
<!-- This would makes the SelectedValue text wrap but the binding binding syntax used is taken literally -->
<TextBlock Text="{Binding SelectedValue, ElementName=CboxWakeUp}" TextWrapping="Wrap" TextAlignment="Center"/>
</Button>
</Grid>
</Window>
I'm close but I need help.
I need to wrap the text of the button and I have figured it out by embedding a TextBlock control inside the Button control (Thank you TheMadTechnician! ) but the binding syntax I used in the button is taken literally.
NOTE: I had the binding to the button working directly but when a selection was made in the ComboBox, Kaxaml gave me "Must disconnect specified child from current parent Visual before attaching to new parent Visual" error. I'm hoping the TextBlock control will side-step this issue.
PS: I was going to try to make the button change colour based on the selection made in the ComboBox:
WakeUp and RDP ==> Green
WakeUp from list ==> Blue
WakeUp and Run ==> Red
This will have to be done in PS code and that's ok
I have a xamgrid and I need to call my viewmodel command to perform some action when user >double click on xamgrid cell(event: celldoubleclicked). How to handle this scenario with >MVVM?
You could use the EventToCommand trigger from MvvmLight.
<ig:XamGrid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="CellDoubleClicked">
<cmd:EventToCommand Command={Binding MyCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ig:XamGrid>
Or the InvokeCommandAction in the Blend sdk:
<ig:XamGrid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="CellDoubleClicked">
<i:InvokeCommandAction Command={Binding MyCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ig:XamGrid>
I am using MVVM in my application and binding combobox to my collection. However when i run it the combobox doesn't have any selected index and it shows a ugly emtpy box. How do i get past this issue?
This is my code :-
<ComboBox x:Name="cmbPasswordQuestion" ItemsSource="{Binding PasswordQuestionList}" DisplayMemberPath="SiteTermsXItemsName" SelectedValuePath="SiteTermsXItemId" SelectedValue="{Binding SignUpUser.PasswordQuestionId}" Margin="97,210,247,0" VerticalAlignment="Top" Height="24">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding PasswordQuestionCommand}" CommandParameter="{Binding SelectedItem, ElementName=cmbPasswordQuestion}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
I am not able to set the SelectedIndex = 0 directly in xaml as i am binding the collection run time.
Thanks in advance :)
All you need is to set SignUpUser.PasswordQuestionId to the id of the first item in the combobox right after you initialized the PasswordQuestionList property. And the binding will do the rest.
I have a user control called HomePage.xaml. I'm creating a model instance (using MVVM pattern) in the code behind file in the constructor of page as
MainViewModel model = new MainViewModel();
I have a button in HomePage.xaml which I want to bind to the command inside MainViewModel called GetData() and want to populate the data in datagrid. MainViewModel has an ObservableCollection which I would use to bind the data in datagrid.
Populating the data in datagrid without binding command works fine.
I'm binding the button as:
<StackPanel x:Name="stkPanelInput" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
<Button x:Name="buttonGetData" Width="70" Content="GetData" Command="{Binding GetData}" Click="buttonGetData_Click"/>
</StackPanel>
How shall I bind the command using MVVM?
Like Archie said, set the DataContext of your page to the instance of your MainViewModel.
DataContext = model;
Then you have your XAML look like this:
<StackPanel x:Name="stkPanelInput" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
<Button x:Name="buttonGetData" Width="70" Content="GetData" Command="{Binding GetDataCommand}" Click="buttonGetData_Click"/>
</StackPanel>