Avalonia Datagrid MouseEvent Binding - avaloniaui

I am a newbie to Avalonia, and I'v searched for many reference.
My purpose is to achieve a recent file lists and it will open the file when I click it.
I tried the DataGrid and it displays my data well, but I wonder how to bind MouseEvent to it.
For key event, I can use KeyBindings to handle, but I don't find such a MouseBindings or Command properties.
Maybe I get into a dead end that I should use ItemRepeater and Button to do such?
<DataGrid Margin="16" RowHeight="24" Items="{Binding $parent[Window].DataContext.RecentFiles}" IsReadOnly="True" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Width="*" Binding="{Binding Name}"/>
<DataGridTextColumn Width="96" Binding="{Binding LastModifiedString}"/>
</DataGrid.Columns>
</DataGrid>

Related

Datagrid is glitching and rows are getting Reordered when scrolling upwards

When I run this, it gets a fairly large list of items. At first scrolling downwards has no issues at all. However when I scroll upwards it starts glitching back and forth and you can never reach the top of the DataGrid. Even more the rows start to get mixed up and no longer sit in the position they're supposed to.
<DataGrid
Grid.Row="1"
CanUserResizeColumns="True"
CanUserReorderColumns="False"
CanUserSortColumns="False"
IsReadOnly="True"
HeadersVisibility="All"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
AutoGenerateColumns="False"
GridLinesVisibility="All"
MaxColumnWidth="500"
HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible"
Background="{Binding Context.UniversalSettingsContext.BackgroundColor}"
Items="{Binding Context.WorkItemPanelContext.WorkItems}"
>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Allow?">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsThreeState="False" IsChecked="{Binding IsChecked}" HorizontalAlignment="Right"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Binding="{Binding Id}" Header="Id" />
<DataGridTextColumn Binding="{Binding State}" Header="State" />
<DataGridTextColumn Binding="{Binding Description}" Header="Description" />
<DataGridTextColumn Binding="{Binding ReleaseNotes}" Header="Release Notes" />
</DataGrid.Columns>
</DataGrid>
The binding statement you have:
Context.WorkItemPanelContext.WorkItems
Is suggesting you have an odd structure behind your view. It needs flattening so you are databinding to WorkItems, not Context.WorkItemPanelContext.WorkItems.
Have a read of the Law of Demeter.
I would suggest having a read around MVVM as a design pattern and reorganise your data so it's a much more binding friendly and flatter than it is now.
It looks like you have one god object called Context that has absolutely everything on it which is not a good design choice.
There isn't anything in this code that suggest it should be behaving like this other than the deep binding I've mentioned.

User control data binding winrt

I am facing problem of data binding in user control.
<UserControl.Resources>
<CollectionViewSource
x:Name="itemsViewSource"
Source="{Binding Items}"
d:Source="{Binding AllGroups[0].Items, Source={d:DesignInstance Type=data:SampleDataSource, IsDesignTimeCreatable=True}}"/>
</UserControl.Resources>
<UserControl.DataContext>
<Binding>
<Binding.Source>
<local:SampleDataSource />
</Binding.Source>
</Binding>
</UserControl.DataContext>
<StackPanel Margin="20,60">
<TextBlock Style="{StaticResource PageHeaderTextStyle}">Categories</TextBlock>
<ListBox>
<ListBoxItem Content="{Binding Title}" />
</ListBox>
</StackPanel>
The Title property binding is showing in the XAML designer window but nothing is showing during execution of the program.
I am using the default flipview layout in which there is a "Title" in SampleDataSource.cs file
The "d:" prefix is for properties that you would define solely for use in the designer, so you should try replacing "d:Source" with "Source".
did you fix following line
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
it's defined at the top where all the page declarations are.
Also
Source="{Binding Items}"
is referring to a List named Items in your DataContext.

Silverlight Datagrid Column header Text Break Line

I am Using Silverlght 4.I have a datagrid Header whose text="StudentDetailOfAplication".Since It Occupies more length in I need to have a break line near "Application".Please send me some usefull links how to do this
<Grid x:Name="LayoutRoot" Background="White">
<sdk:DataGrid AutoGenerateColumns="False" Height="227" HorizontalAlignment="Left" Margin="39,37,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="316" >
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="StudenDetailsOFApplication" Width="100" Binding="{Binding id}"></sdk:DataGridTextColumn>
<sdk:DataGridTextColumn Header="Name" Width="100" Binding="{Binding productName}"></sdk:DataGridTextColumn>
<sdk:DataGridTextColumn Header="NumberOfAplication" Width="114" Binding="{Binding qty}"></sdk:DataGridTextColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid></Grid>
Header="Student Details
Of Application" or Header="Student Details
Of Application" will also work. Or if you're not inserting into a string you can also do something like.
<TextBlock>
Line<LineBreak/>Break
</TextBlock>
but personally I just use the codes shown in the first two examples. Also keep in mind the xml will retain whitespace, so you can do like Header=" Details of
Student Application" for alignment purposes if TextAlignment property isnt available. Hope this helps.

How to specify Command binding scope in MVVM?

I'm messing around with MVVM and I've hit a bit of a roadblock with binding commands to buttons. I have a few buttons in a View ( = UserControl) that are generated based on a list of objects I have.
My code looks like this:
(MainWindow)
<ItemsControl ItemsSource="{Binding ViewModels}" Margin="12,57,12,12" />
(UserControl)
<ItemsControl ItemsSource="{Binding AllConnections}" Margin="0,34,0,0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Password}" Height="23" HorizontalAlignment="Left" Margin="114,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Command="{Binding Path=ConnectCommand}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
But ConnectCommand isn't being called and I assume it's because XAML is looking for it in the AllConnections binding, rather than the ViewModels binding where it should. How do I specify this?
You should use Relative source to specify the ancestor. Something like this:
Command = "{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=ViewModel.CommandToBind}"
You can add your command to Resources and just use {StaticResource yourCommand}. This could significally simplify xaml.
Useful links: WPF Commands, How to declare Application level commands?Commands as XAML ResourcesMVVM Commanding inside of a datatemplate

Share context menu in WP7 xaml

I want to add a contextmenu to items in a listbox. Typically the answer is to add the contextmenu to the root of the item template. However, I am using a template selector, so there are multiple templates in use, depending on the data of each item in the listbox. This means I need to add the same contextmenu definition to each template, which is not very appealing.
One solution is to wrap the datatemplate in a ContentControl, which would give me a single place for the context menu definition. However, I believe this would add layout overhead that isn't necessary.
Another solution I tried is adding the ContextMenu to the resource dictionary, but I believe this ends up sharing the same object instance across all the uses, and due to the way ContextMenu is implemented, this also does not work.
A third solution is using the Loaded event to call a function which populates the context menu appropriately. However, this ends up moving a lot of code that ought to be in the XAML into code, and looks quite ugly. If there's some way of defining the context menu in xaml, and then just referencing that from code, I would find it appealing, but I don't quite see how to do that.
What is the right way to share the same ContextMenu across the templates in a template selector?
This is the ContentControl method, which works, but ends up adding two content controls to each item:
<ListBox.ItemTemplate>
<DataTemplate>
<ContentControl>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu Loaded="ContextMenu_Loaded">
<toolkit:MenuItem Header="Delete"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<ContentControl ContentTemplate="{StaticResource MyTemplate}" Content="{Binding}"/>
</ContentControl>
</DataTemplate>
</ListBox.ItemTemplate>
How about adding the ContextMenu to the TemplateSelector?
<ListBox ItemsSource="{Binding}">
<ListBox.Resources>
<DataTemplate x:Key="MyTemplate">
<StackPanel>
<TextBlock Text="{Binding}" />
</StackPanel>
</DataTemplate>
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<local:CustomTemplateSelector Content="{Binding}">
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Header="Delete"
Click="MenuItem_Click" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<local:CustomTemplateSelector.TemplateOne>
<DataTemplate>
<ContentControl Content="{Binding}"
ContentTemplate="{StaticResource MyTemplate}"
Foreground="Blue" />
</DataTemplate>
</local:CustomTemplateSelector.TemplateOne>
<local:CustomTemplateSelector.TemplateTwo>
<DataTemplate>
<ContentControl Content="{Binding}"
ContentTemplate="{StaticResource MyTemplate}"
Foreground="Red" />
</DataTemplate>
</local:CustomTemplateSelector.TemplateTwo>
<local:CustomTemplateSelector.TemplateThree>
<DataTemplate>
<ContentControl Content="{Binding}"
ContentTemplate="{StaticResource MyTemplate}"
Foreground="Yellow" />
</DataTemplate>
</local:CustomTemplateSelector.TemplateThree>
</local:CustomTemplateSelector>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I ran this and it worked for me - give it a try and let me know if this was the effect you were looking for or not.