Treeview selectedItem highlight on startup - c#

I have a treeview in my xaml as below. I use the selected item by using interactivity and bind the event.
<DataTemplate x:Key="TreeTemplate">
<TreeView Name="TreeView" ItemsSource="{Binding ItemList}" ItemTemplate="{StaticResource ChildTemplate}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedItemChanged">
<i:InvokeCommandAction
Command="{Binding SetSelectedItemCommand}"
CommandParameter="{Binding SelectedItem, ElementName=TreeView}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TreeView>
</DataTemplate>
This all works very well except when by application loads for the first time. Even if SelectedItem property is set on startup, the treeview does not show the item highlighted unless a mouse event is fired which causes SelectedItemChanged event.
Any ideas as as to how can I do this?
My datacontext is in code behind of the xaml
myView = new MyViewModel();
this.DataContext = myView;
InitializeComponent();

This will work:
<DataTemplate x:Key="TreeTemplate">
<TreeView Name="TreeView" ItemsSource="{Binding ItemList}" ItemTemplate="{StaticResource ChildTemplate}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedItemChanged">
<i:InvokeCommandAction
Command="{Binding SetSelectedItemCommand}"
CommandParameter="{Binding SelectedItem, ElementName=TreeView}"/>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction
Command="{Binding SetSelectedItemCommand}"
CommandParameter="{Binding SelectedItem, ElementName=TreeView}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TreeView>
</DataTemplate>

Related

Triggering a command with a command target

*I'm using Material Design though this shouldn't be relative to the question
<ScrollViewer>
<ListView>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListView>
</ScrollViewer>
Sofar I've gotten it to trigger the command on double click, although I want to also set a command target property like:
CommandTarget="{Binding ElementName=addhost}"

Hide specific context menu in datagrid upon selected row wpf mvvm

I got one issue on datagrid using WPF Mvvm. I set context menu on datagrid.
here is my code.
<DataGrid.ContextMenu>
<ContextMenu IsEnabled="{Binding IsEnableCaseRefNo}"
DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}" >
<MenuItem Header=" - View Case" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ContextCommand}" CommandParameter="VCD"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</MenuItem>
<MenuItem Header=" - Cheque" Visibility="{Binding SyncColumnVisibility, Converter={StaticResource visibilityConverter}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ContextCommand}" CommandParameter="BMK"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</MenuItem>
<MenuItem Header=" - Cash" Visibility="{Binding SyncColumnVisibility, Converter={StaticResource visibilityConverter}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ContextCommand}" CommandParameter="UNBMK"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</MenuItem>
</ContextMenu>
</DataGrid.ContextMenu>
I can show and hide context menu using visibility property. but now i want to enable/disable "-Cheque" context menu upon user selection. how can I disable 'Cheque' context menu when there are 100 over dollars in donate columns(that already shown in datagrid).
Here is my datagrid :
<DataGrid Name="dgv" Background="WhiteSmoke" AutoGenerateColumns="False" CanUserAddRows="False" SelectedItem="{Binding SelectedItems,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
CanUserDeleteRows="False" Grid.Row="2" ItemsSource="{Binding LstcaseHearingModel,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Grid.Column="2" HorizontalAlignment="Stretch" >
Thanks for any help.
frog
Use SelectedCellsChanged event to get desired item.
private void Dgrd_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
SomeEntity item = (SomeEntity) Dgrd.CurrentItem;
if(item.Donate > 100)
viewModel.SyncColumnVisibility = Visibility.Collapsed;
else
viewModel.SyncColumnVisibility = Visibility.Visible;
}
As you are binding SelectedItem property to SelectedItems property of your ViewModel. So you can check the condition in its setter.

RelativeSource binding can't find Frame ancestor

I'm trying to pass as a CommandParameter the actual Frame object to which I'm applying the Command
XAML
<Frame NavigationUIVisibility="Hidden" Source="{Binding TargetContentPage}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="ContentRendered">
<command:EventToCommand Command="{Binding ContentRendered}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=Frame}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Frame>
C#
public RelayCommand<Frame> ContentRendered
{
get
{
return new RelayCommand<Frame>(frame => frame.RemoveBackEntry());
}
}
When running, a NullReferenceException is raised saying frame is null.
What is possibly wrong in the code above ?
There is no ancestor/descendent relation between Frame and EventToCommand. Use an ElementName binding instead:
<Frame x:Name="frame" ...>
<i:Interaction.Triggers>
<i:EventTrigger EventName="ContentRendered">
<command:EventToCommand
Command="{Binding ContentRendered}"
CommandParameter="{Binding ElementName=frame}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Frame>

Event Binding doesn't work in ContentPresenter

I'm want to use Interaction.Triggers inside a ContentPresenter, but for some reasons, the event doesn't get fired.
If I change ContentPresenter against ContentControl, the event binding works as expected.
<DataTemplate x:Key="MapPictureTemplate">
<Grid Width="80" Height="80" x:Name="Grid">
<Border
Background="{Binding Thumbnail, Converter={StaticResource StreamToImageBrushConverter}}"
CornerRadius="40">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<i:InvokeCommandAction Command="{Binding Source={StaticResource Locator}, Path=Main.ImageTappedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Border>
</Grid>
</DataTemplate>
and the ContentControl creation in code behind:
var content = new ContentPresenter()
{
Content = this,
ContentTemplate = Application.Current.Resources["MapPictureTemplate"] as DataTemplate
};

WPF ListBox selection

I have a problem with extended selection in ListBox. Let's say I have a ListBox with 10 items and I'm selecting first 5 of them using Shift button. The SelectionChanged event is fired with 5 items. After that I want to select 3 items from those 5, again with Shift button pressed, but SelectionChanged event is not fired. How can I react to the second selection of items, when I'm selecting 3 of those 5 previously selected ones?
can you show the xaml code as well I would like to see what your binding looks like
it should looks something like this.. but I can't be certain unless I see your code
In Command binding you have used binding which has relative source binding...
consider making these changes in binding
1) using list box as Ancestortype
2) While binding use Path=DataContext.SelectionChangedCommand otherwise it will take list box as datacontext.
<catel:EventToCommand Command="{Binding Path=DataContext.SelectionChangedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}" DisableAssociatedObjectOnCannotExecute="False" PassEventArgsToCommand="True" />
here is an example of what the XAML would look like for ListBoxItem Template
<Grid>
<StackPanel Orientation="Horizontal">
<Label Width="180">Field1</Label>
<ListBox Height="200"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding List1, Mode=OneWay}"
Name="listBox1"
SelectionMode="Single"
Width="300">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="290">
<TextBlock Width="90" Text="{Binding}"></TextBlock>
<ComboBox Width="180" ItemsSource="{Binding DataContext.List2, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" DisplayMemberPath="Field1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<catel:EventToCommand Command="{Binding Path=DataContext.SelectionChangedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}" DisableAssociatedObjectOnCannotExecute="False" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>

Categories