Context Menu MenuItem Click Data Context? - c#

I have a collection of objects to which I have created a ScrollViewer to show. For each Object, the data is displayed in a grid to which I have added a context menu so that when the user right clicks the grid, they can alter properties of the object. I cannot figure out how to know which object has been right clicked.
In the code below, how would I know which "VoltageMonitor" was right clicked when it reaches the callback for MenuItem "TurnOff"?
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Hidden" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.ColumnSpan="2" Margin="0,0,0,15">
<ItemsControl Name="icVoltageMonitorsPanel" ItemsSource="{Binding AppMonitors.VoltageMonitors, Mode=TwoWay}" Grid.ColumnSpan="2" Padding="0" Margin="0" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ScrollViewer}}, Path=ActualWidth}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="local:VoltageMonitor">
<Grid Height="130" Name="grdIndicator" Width="200" MinWidth="200" MinHeight="130">
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="Edit Format..." />
<MenuItem Header="Turn off" Click="MenuItem_Click" DataContext=""/>
</ContextMenu>
</Grid.ContextMenu>
<Grid.RowDefinitions>
<RowDefinition Height="30*" />
<RowDefinition Height="25*" />
<RowDefinition Height="25*" />
<RowDefinition Height="25*" />
<RowDefinition Height="25*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="133*" />
<ColumnDefinition Width="67*" />
</Grid.ColumnDefinitions>

Assuming you have defined an ICommand in your viewmodel, you can do it like this:
<ItemsControl ...>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="local:VoltageMonitor">
<Grid ..>
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="Edit Format..." />
<MenuItem Header="Turn off"
CommandParameter="{Binding}"
Command="{Binding RelativeSource=
{RelativeSource Mode=FindAncestor,
AncestorType={x:Type ItemsControl}},
Path=DataContext.TurnOffCommand}"/>
</ContextMenu>
</Grid.ContextMenu>

Related

scrollbar not working when scrolling down it goes back up in c#

I have a screen in xaml that contains a TabControl, this is divided into 2 tabs and each tab has a dataGrid inside. Due to the amount of data that is handled in the datagrid, the use of scrollBar is required and the client's request is that when selecting a row in a tab, it must be selected in tab 2 and that the scrollbar go down to the position where it is. the selected row, for this I modified the codeBehind with the following code.
The problem that it generates for me is that when I want to lower the scrollbar in tab 1 when I have already selected a row, the scrollbar always returns to the position of the selected row without allowing it to go down or up as far as I want. Please help
XAML.cs
private void UexIndividuos_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if((DataContext as VistaTabularViewModel).SelectedUEX != null)
{
var uex = (DataContext as VistaTabularViewModel).SelectedUEX;
//tabIndividuos.IsSelected = true;
DG_Individuos.SelectedItem = uex;
DG_Individuos.UpdateLayout();
DG_Individuos.ScrollIntoView(uex);
}
}
private void UexMediciones_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if ((DataContext as VistaTabularViewModel).FilaMedicionSeleccionada != null)
{
var uex = (DataContext as VistaTabularViewModel).FilaMedicionSeleccionada;
DG_Mediciones.SelectedItem = uex;
DG_Mediciones.UpdateLayout();
DG_Mediciones.ScrollIntoView(uex);
}
}
Codigo XAML
<common:DialogWindowBase
x:Class="Biocap.View.VistaTabular.VistaTabularView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
xmlns:common="clr-namespace:Biocap.Common"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:local="clr-namespace:Biocap.View.VistaTabular"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="VistaTabularEnsayoChildWindow"
Title="{Binding TituloPantalla, UpdateSourceTrigger=PropertyChanged}"
Dialog:DialogParticipation.Register="{Binding}"
d:DesignHeight="450"
d:DesignWidth="800"
CloseButtonCommand="{Binding CloseButtonCommand}"
CloseByEscape="False"
ShowCloseButton="True"
mc:Ignorable="d">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded" SourceObject="{Binding ElementName=VistaTabularEnsayoChildWindow}">
<i:InvokeCommandAction Command="{Binding MantenedorLoadedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Controls:MetroContentControl
Grid.RowSpan="9"
VerticalAlignment="Top"
Panel.ZIndex="99"
Content="{Binding ErrorViewModel}" />
<TabControl Width="1100" Height="570">
<TabItem Header="{Binding HeaderTabEnsayo}" Name="tabIndividuos">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<GroupBox Margin="5" Header="Individuos" >
<DataGrid
x:Name="DG_Individuos"
AutoGenerateColumns="True"
AutoGeneratingColumn="DataGridIndividuos_AutoGeneratingColumn"
CanUserAddRows="False"
ItemsSource="{Binding IndividuosCollectionView, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedItem="{Binding SelectedUEX, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectionMode="Extended"
ScrollViewer.CanContentScroll="True"
VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.ScrollUnit="Pixel"
VirtualizingPanel.IsContainerVirtualizable="True"
VirtualizingPanel.VirtualizationMode="Recycling"
IsReadOnly="{Binding Guardar_uex, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectionUnit="FullRow" SelectionChanged="UexIndividuos_SelectionChanged">
<i:Interaction.Triggers>
<i:EventTrigger EventName="BeginningEdit">
<i:InvokeCommandAction Command="{Binding CeldaBeginningEdit}" PassEventArgsToCommand="True" />
</i:EventTrigger>
<i:EventTrigger EventName="CellEditEnding">
<i:InvokeCommandAction Command="{Binding IndividuosCellEditEnding}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
</GroupBox>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button
Margin="5"
Command="{Binding AgregarUnidadExperimental}"
Content="Agregar unidad experimental" />
<Button
Grid.Column="1"
Margin="5"
Command="{Binding EliminarUnidadExperimental}"
CommandParameter="{Binding ElementName=DG_Individuos}"
Content="Eliminar unidades experimentales seleccionadas"
ToolTip="Eliminar unidad experimental nueva si aún no se han guardado los cambios" />
</Grid>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button
Grid.Column="0"
Margin="5"
Background="{StaticResource MahApps.Brushes.Accent}"
Command="{Binding GuardarCambiosIndividuos}"
CommandParameter="{Binding ElementName=DG_Individuos}"
Content="Guardar cambios individuos" />
<Button
Grid.Column="1"
Margin="5"
Command="{Binding DescartarYSalirCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type common:DialogWindowBase}}}"
Content="Cerrar ventana" />
</Grid>
</Grid>
</TabItem>
<TabItem Header="Mediciones" Name="tabMediciones">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="auto" />
<RowDefinition />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<GroupBox Margin="5" Header="Sesiones de medición">
<DataGrid
x:Name="DG_SesionesMedicion"
AutoGenerateColumns="True"
AutoGeneratingColumn="DataGrid_AutoGeneratingColumn"
CanUserAddRows="False"
ItemsSource="{Binding ListaMedicionesResumenCollectionView, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedItem="{Binding MedicionResumenSeleccionada, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedIndex="{Binding SelectFirstItem,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
SelectionMode="Single"
SelectionUnit="FullRow">
<i:Interaction.Triggers>
<i:EventTrigger EventName="BeginningEdit">
<i:InvokeCommandAction Command="{Binding CeldaBeginningEdit}" PassEventArgsToCommand="True" />
</i:EventTrigger>
<i:EventTrigger EventName="CellEditEnding">
<i:InvokeCommandAction Command="{Binding MedicionResumenCellEditEnding}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
</GroupBox>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button
Margin="5"
Command="{Binding AgregarMedicionResumen}"
Content="Agregar sesión de medición" />
<Button
Grid.Column="1"
Margin="5"
Command="{Binding EliminarMedicionResumenVacia}"
Content="Eliminar sesión de medición vacía" />
</Grid>
<GroupBox
Grid.Row="2"
Margin="5"
Header="Mediciones">
<DataGrid
x:Name="DG_Mediciones"
AutoGenerateColumns="True"
AutoGeneratingColumn="DataGrid_AutoGeneratingColumn"
CanUserAddRows="False"
CanUserReorderColumns="False"
ScrollViewer.CanContentScroll="True"
VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.ScrollUnit="Pixel"
VirtualizingPanel.IsContainerVirtualizable="True"
VirtualizingPanel.VirtualizationMode="Recycling"
ItemsSource="{Binding MedicionesCollectionView, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectedItem="{Binding FilaMedicionSeleccionada, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
SelectionMode="Extended" SelectionChanged="UexMediciones_SelectionChanged">
<i:Interaction.Triggers>
<i:EventTrigger EventName="BeginningEdit">
<i:InvokeCommandAction Command="{Binding CeldaBeginningEdit}" PassEventArgsToCommand="True" />
</i:EventTrigger>
<i:EventTrigger EventName="CellEditEnding">
<i:InvokeCommandAction Command="{Binding MedicionesCellEditEnding}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
</GroupBox>
<Grid Grid.Row="3">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button
Margin="5"
Command="{Binding EliminarMedicionSeleccionada}"
CommandParameter="{Binding ElementName=DG_Mediciones}"
Content="Eliminar fila detalle medición seleccionada" />
<Button
Grid.Column="1"
Margin="5"
Command="{Binding ActualizarVariablesCalculadas}"
Content="Actualizar V. Calculadas" />
</Grid>
<Grid Grid.Row="4">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button
Margin="5"
Background="{StaticResource MahApps.Brushes.Accent}"
Command="{Binding GuardarCambiosMediciones}"
CommandParameter="{Binding ElementName=DG_Mediciones}"
Content="Guardar cambios mediciones" />
<Button
Grid.Column="1"
Margin="5"
Command="{Binding DescartarYSalirCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type common:DialogWindowBase}}}"
Content="Cerrar ventana" />
</Grid>
</Grid>
</TabItem>
</TabControl>
</Grid>
</common:DialogWindowBase>

How to add a button in a NavigationView using UWP?

I'm implementing a NavigationView like described is this topic.
Between the nav items (NavigationViewItem) I need to add a simple button, that will just execute an action, it will not navigate anywhere.
Every control I add to nav receives the property to stay selected when clicked, but I need a control that doesn't deselect the current nav, just execute an action.
Does anyone know how to do this or can suggest a solution? Grateful.
but I need a control that doesn't deselect the current nav, just execute an action.
Sure, you could edit the default NavigationView style and insert button into PaneContentGrid, and you could get default NavigationView style from generic.xaml file, then find PaneContentGrid add button like the following (InsertButton).
<SplitView.Pane>
<Grid x:Name="PaneContentGrid" Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.LeftPaneVisibility}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="0" />
<!-- above button margin + back button space -->
<RowDefinition x:Name="PaneContentGridToggleButtonRow" Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="8" />
<!-- above list margin -->
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="8" />
</Grid.RowDefinitions>
<Grid x:Name="ContentPaneTopPadding" Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.TopPadding}" />
<Grid Grid.Row="2" Height="{StaticResource PaneToggleButtonHeight}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{ThemeResource PaneToggleButtonWidth}" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ContentControl
x:Name="PaneHeaderContentBorder"
Grid.Column="1"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
IsTabStop="False" />
</Grid>
<Grid
x:Name="AutoSuggestArea"
Grid.Row="3"
Height="{ThemeResource NavigationViewTopPaneHeight}"
VerticalAlignment="Center">
<ContentControl
x:Name="PaneAutoSuggestBoxPresenter"
Margin="{ThemeResource NavigationViewAutoSuggestBoxMargin}"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center"
IsTabStop="False" />
<Button
x:Name="PaneAutoSuggestButton"
Width="{TemplateBinding CompactPaneLength}"
Style="{ThemeResource NavigationViewPaneSearchButtonStyle}"
Visibility="Collapsed" />
</Grid>
<ContentControl
x:Name="PaneCustomContentBorder"
Grid.Row="4"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
IsTabStop="False" />
<!-- Left nav list -->
<NavigationViewList
x:Name="MenuItemsHost"
Grid.Row="6"
Margin="0,0,0,20"
HorizontalAlignment="Stretch"
IsItemClickEnabled="True"
ItemContainerStyle="{TemplateBinding MenuItemContainerStyle}"
ItemContainerStyleSelector="{TemplateBinding MenuItemContainerStyleSelector}"
ItemTemplate="{TemplateBinding MenuItemTemplate}"
ItemTemplateSelector="{TemplateBinding MenuItemTemplateSelector}"
SelectedItem="{TemplateBinding SelectedItem}"
SelectionMode="Single"
SingleSelectionFollowsFocus="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.SingleSelectionFollowsFocus}" />
<ContentControl
x:Name="FooterContentBorder"
Grid.Row="7"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
IsTabStop="False" />
<Button Grid.Row="7" Content="Command" x:Name="InsertButton"/>
<NavigationViewItem
x:Name="SettingsNavPaneItem"
Grid.Row="8"
Icon="Setting" />
</Grid>
</SplitView.Pane>
If you don't want to edit style, you could also insert button into NavigationView PaneFooter like following
<NavigationView x:Name="nvSample">
<NavigationView.MenuItems>
<NavigationViewItem
Content="Menu Item1"
Icon="Play"
Tag="SamplePage1" />
<NavigationViewItem
Content="Menu Item2"
Icon="Save"
Tag="SamplePage2" />
<NavigationViewItem
Content="Menu Item3"
Icon="Refresh"
Tag="SamplePage3" />
<NavigationViewItem
Content="Menu Item4"
Icon="Download"
Tag="SamplePage4" />
</NavigationView.MenuItems>
<Frame x:Name="contentFrame" />
<NavigationView.PaneFooter>
<Button Content="Click"/>
</NavigationView.PaneFooter>
</NavigationView>

How to make StackPanel to fill his container with and height in WPF

I'm really new to WPF and I am trying to make an app that will display Word,PDF, and Excel files in it. The left side of the window will show the content of the uploaded files in my app and the right side should have a list of all the uploaded files in a ScrollViewer for the user to select what to be displayed.
Everything is responsive and resize correctly to different window
size, but the scroll viewer is fixed and doesn't resize. I want my
ScrollViewer to always be at max width and height of his container, so
that it resizes correctly on what ever the window size is.
I have tried different approaches, but all failed and now I have no idea how to do it.
Here is my code:
<Window x:Class="PPMGNews.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PPMGNews"
mc:Ignorable="d"
Title="CoolAppForNews" Height="450" Width="800" Icon="./Resources/Logoto.ico">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="1*" />
<RowDefinition Height="2*" />
<RowDefinition Height="2*" />
<RowDefinition Height="2*" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Menu Grid.Row="1" Grid.ColumnSpan="6" BorderBrush="#FFAEA6A6" BorderThickness="0,0,0,1">
<MenuItem Header="Add">
<MenuItem Header="_Word" x:Name="addWord" Click="AddWord_Click"/>
<MenuItem Header="_PDF"/>
<MenuItem Header="_Excel"/>
</MenuItem>
<MenuItem Header="Delete">
<!--<MenuItem Header="_Open"/>
<MenuItem Header="_Close"/>
<MenuItem Header="_Save"/> -->
</MenuItem>
<MenuItem Header="Edit">
<!--<MenuItem Header="_Open"/>
<MenuItem Header="_Close"/>
<MenuItem Header="_Save"/>-->
</MenuItem>
</Menu>
<DocumentViewer x:FieldModifier="public" x:Name="docViwer"
Grid.Row="2" Grid.RowSpan="4" Grid.ColumnSpan="4"
BorderBrush="Black" BorderThickness="1"
Margin="1,2,40,1">
</DocumentViewer>
<Border Grid.Row="2" Grid.RowSpan="5" Grid.Column="4"
CornerRadius="0" BorderBrush="Gray"
BorderThickness="2" Margin="-35,0,0,0">
<StackPanel Grid.Row="2" Grid.RowSpan="5" Grid.Column="4"
Height="auto" Width="auto">
<Label HorizontalAlignment="Center" FontSize="17">News</Label>
<ScrollViewer VerticalScrollBarVisibility="Visible"
HorizontalScrollBarVisibility="Disabled"
HorizontalAlignment="Left">
<StackPanel>
<Label>Some News</Label>
<Label>Some News</Label>
<Label>Some News</Label>
<Label>Some News</Label>
<Label>Some News</Label>
<Label>Some News</Label>
<Label>Some News</Label>
<Label>Some News</Label>
</StackPanel>
</ScrollViewer>
</StackPanel>
</Border>
</Grid>
</Window>
The idea is that the app should fill the ScrollViewer with content in the backend whenever a user adds new files. And then attach click events on the content to change the DocumentViewer document.
You should use on the controls which you want to dock inside their respective parent controller.
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"

Bindings from Multiple ViewModels in same View WPF MVVM

I have a WPF MVVM project that includes a View which contains several controls.
There is a DockPanel which contains all the controls in the View with DataContext to a MainViewModel Class, and inside of it there is a Grid with DataContext to other object ViewModel which it's property contained in the MainViewModel Class.
Now, in that Grid I have a 2 buttons surrounds with WrapPanel, and the buttons have a Commands to a property which wrote in the MainViewModel Class, and when I pressed on it nothing happens (Because the Grid's DataContext is to other object).
I need the Commands to be stay in the MainViewModel Class, How can I do that?
XAML: (just the relevant parts)
<DockPanel x:Name="MonitorParent" DataContext="{Binding MonitorMainViewModel}" LastChildFill="False" Width="1144" HorizontalAlignment="Left">
.......
<StackPanel>
<ContentControl Visibility="{Binding Path=NoMonitorsMessageVisibility, Converter={StaticResource visibilityConverter}}">
<StackPanel Width="804" Height="574">
<Grid DataContext="{Binding CurrentMonitor}" Margin="10 0" Height="auto">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" MinHeight="23" />
<RowDefinition Height="Auto" MinHeight="23" />
<RowDefinition Height="Auto" MinHeight="23" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="15" Content="Serial Number" x:Name="SerialNumber" />
<WrapPanel DataContext="{Binding MonitorMainViewModel}" HorizontalAlignment="Center" Margin="0,10,0,10" Grid.ColumnSpan="2" Grid.Row="6" >
<Button Content="Add" Width="60" Margin="0,0,40,0" Command="{Binding AddConfidenceCommand}" />
<Button Content="Delete" Width="60" Margin="30,0,0,0" Command="{Binding DeleteConfidenceCommand}"/>
</WrapPanel>
........
</DockPanel
You could use the name of the DockPanel for binding like this:
<DockPanel x:Name="MonitorParent" DataContext="{Binding MonitorMainViewModel}" LastChildFill="False" Width="1144" HorizontalAlignment="Left">
.......
<StackPanel>
<ContentControl Visibility="{Binding Path=NoMonitorsMessageVisibility, Converter={StaticResource visibilityConverter}}">
<StackPanel Width="804" Height="574">
<Grid DataContext="{Binding CurrentMonitor}" Margin="10 0" Height="auto">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" MinHeight="23" />
<RowDefinition Height="Auto" MinHeight="23" />
<RowDefinition Height="Auto" MinHeight="23" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="15" Content="Serial Number" x:Name="SerialNumber" />
<WrapPanel DataContext="{Binding MonitorMainViewModel}" HorizontalAlignment="Center" Margin="0,10,0,10" Grid.ColumnSpan="2" Grid.Row="6" >
<Button Content="Add" Width="60" Margin="0,0,40,0" Command="{Binding ElementName=MonitorParent, Path=DataContext.AddConfidenceCommand}" />
<Button Content="Delete" Width="60" Margin="30,0,0,0" Command="{Binding ElementName=MonitorParent, Path=DataContext.DeleteConfidenceCommand}"/>
</WrapPanel>
........
Try this:
<Button Content="Add" Width="60" Margin="0,0,40,0"
Command="{Binding DataContext.AddConfidenceCommand, ElementName=MonitorParent}" />
Or:
<Button Content="Add" Width="60" Margin="0,0,40,0"
Command="{Binding DataContext.AddConfidenceCommand, RelativeSource={RelativeSource AncestorType=DockPanel}}" />

Gridsplitter unwanted behavior

It is my first time working with a GridSplitter in WPF. I am trying to allow the user to resize the grid that contains 2 main controls. On top is a datagrid, below is a button with a map image over it. This is what it looks like in the designer. I have a separate row just for the gridsplitter. The xaml for the row definitions is
<Grid.RowDefinitions>
<RowDefinition Height="37" />
<RowDefinition Height="274*" />
<RowDefinition Height="13*"/>
<RowDefinition Height="272*"/>
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
Interestingly, the GridSplitter seems to work if I'm moving it down. Then I can move it as needed and functions correctly. However, it seems that if I move the splitter above where the line for the row defined above it is, I am unable to move it down again. I can only continue to move it further up.
I have attached a link to a gif showing the behavior that I talk about here.
I am wondering how to go about making the GridSplitter function the same when moving it up or down, as right now, the functionality is correct only if it is moved down from it's initial starting position.
Any help is appreciated.
Update: Here is the full xaml for the user control holding the grid:
<UserControl x:Class="LWDCM.Views.JobsControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:LWDCM.Views"
xmlns:Properties="clr-namespace:LWDCM.Properties"
xmlns:Utility="clr-namespace:LWDCM.Utility"
xmlns:Styles="clr-namespace:LWDCM.Styles"
xmlns:gif="https://github.com/XamlAnimatedGif/XamlAnimatedGif"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:Converters="clr-namespace:LWDCM.Converters"
mc:Ignorable="d"
Name="jobsControl"
MinWidth="800"
MinHeight="600"
>
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Styles/AppStyles.xaml" />
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<!-- Accent and AppTheme setting -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid x:Name="MainGrid"
Margin="0,0,0,0"
Background="#26598c"
>
<!--<Grid.Background>
<LinearGradientBrush ColorInterpolationMode="SRgbLinearInterpolation"
StartPoint="0.5, 0.0"
EndPoint="0.5, 1.0">
<GradientStopCollection>
<GradientStop Color="#FF111111"
Offset="0.0" />
<GradientStop Color="#FF333333"
Offset="0.5" />
<GradientStop Color="#FF111111"
Offset="1.0" />
</GradientStopCollection>
</LinearGradientBrush>
</Grid.Background>-->
<Grid.RowDefinitions>
<RowDefinition Height="37" />
<RowDefinition Height="274*" />
<RowDefinition Height="13*"/>
<RowDefinition Height="272*"/>
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
<Grid.Resources>
<ContextMenu x:Key="ListViewContextMenu">
<MenuItem x:Name="ContextRename"
Header="{x:Static Properties:Resources.RenameJob}"
Command="{Binding RenameJobCommand}" />
<MenuItem x:Name="ContextSetJobsiteAddress"
Header="{x:Static Properties:Resources.SetJobsiteAddress}"
Command="{Binding UpdateJobSiteAddressCommand}" />
<MenuItem x:Name="ContextSetCustomerAddress"
Header="{x:Static Properties:Resources.SetCustomerAddress}"
Command="{Binding UpdateCustomerInformationCommand}" />
<MenuItem x:Name="ContextSetContractorAddress"
Header="{x:Static Properties:Resources.SetContractorAddress}"
Command="{Binding UpdateContractorInformationCommand}" />
<MenuItem x:Name="ContextEditWorkOrderNumber"
Header="{x:Static Properties:Resources.EditWorkOrderNumber}"
Command="{Binding EditWorkOrderNumberCommand}" />
<Separator/>
<MenuItem x:Name="ExportToKML"
Header="{x:Static Properties:Resources.ExportToKML}"
Command="{Binding ExportToKMLCommand}"/>
</ContextMenu>
<Converters:NullVisibilityConverter x:Key="NullVisibilityConverter" />
</Grid.Resources>
<DataGrid x:Name="JobListView"
AutoGenerateColumns="False"
ItemsSource="{Binding UnitStatusCollection, Mode=TwoWay}"
CanUserDeleteRows="False"
Style="{StaticResource JobGridViewStyle}"
SelectedItem="{Binding JobsListViewSelectedUnitInfo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Utility:DataGridColumnsBehavior.BindableColumns="{Binding DataGridColumns}"
ContextMenu="{StaticResource ListViewContextMenu}"
Margin="10,0,10,2"
Grid.Row="1"
SelectionMode="Single"
SelectionUnit="FullRow"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
RowStyle="{StaticResource DataGridRowStyle}"
CellStyle="{StaticResource DataGridCellStyle}"
AlternationCount="2"
CanUserResizeRows="False"
HorizontalGridLinesBrush="#d6d6d6"
VerticalGridLinesBrush="#d6d6d6"
Background="#EAEAEA"
>
<!--This is to allow double clicks to open a job in LWD 3x-->
<DataGrid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick"
Command="{Binding OpenInLWD3xCommand}" />
<KeyBinding Key="Return"
Command="{Binding OpenInLWD3xCommand}" />
<KeyBinding Key="F5"
Command="{Binding RefreshCommand}"/>
</DataGrid.InputBindings>
<DataGrid.Resources>
<Converters:DoubleNanVisibilityConverter x:Key="DoubleNanVisibilityConverter" />
<Converters:NullVisibilityConverter x:Key="NullVisibilityConverter" />
</DataGrid.Resources>
</DataGrid>
<Button Cursor="Hand"
Grid.ZIndex="0"
Margin="10,2,10,1"
Grid.Row="3"
x:Name="cmdMapImage"
Visibility="{Binding JobsListViewSelectedUnitInfo, Converter={StaticResource NullVisibilityConverter}}"
Style="{StaticResource MapButtonStyle}"
Command="{Binding ShowMapOnlineCommand}">
<Image x:Name="mapImage"
Source="{Binding DisplayedImage}"
Tag="{Binding JobId}"
Stretch="Fill"
VerticalAlignment="Center"
RenderOptions.BitmapScalingMode="HighQuality">
</Image>
</Button>
<Rectangle Fill="#26598c"
Grid.ZIndex="1"
Margin="0,10,7,0"
Grid.Row="3"
Height="46"
VerticalAlignment="Top"
HorizontalAlignment="Right"
Width="226"
RadiusY="3.667"
RadiusX="3.667"
Focusable="False"/>
<Button Grid.ZIndex="2"
Command="{Binding ScanForwardCommand}"
x:Name="scrollRightButton"
Margin="0,20,15,0"
HorizontalAlignment="Right"
Width="30"
Height="26"
VerticalAlignment="Top"
Grid.Row="3">
<Image x:Name="scrollRight"
Source="/Assets/Down-30px-tall.png"
Stretch="Fill"
VerticalAlignment="Center"
RenderOptions.BitmapScalingMode="HighQuality">
</Image>
</Button>
<Button Grid.ZIndex="2"
Command="{Binding ScanBackwardCommand}"
x:Name="scrollLeftButton"
Margin="0,20,50,0"
RenderTransformOrigin="1,-0.617"
HorizontalAlignment="Right"
Width="30"
Height="26"
VerticalAlignment="Top"
Grid.Row="3">
<Image x:Name="scrollLeft"
Source="/Assets/Up-30px-tall.png"
Stretch="Fill"
VerticalAlignment="Center"
RenderOptions.BitmapScalingMode="HighQuality">
</Image>
</Button>
<ComboBox x:Name="ScanSizesComboBox"
Grid.ZIndex="2"
ItemsSource="{Binding ScanSizes}"
SelectedItem="{Binding SelectedScanSize, Mode=TwoWay}"
ToolTip="{Binding (Validation.Errors)[0].ErrorContent}"
Margin="0,20,85,0"
HorizontalAlignment="Right"
Width="61"
Height="26"
VerticalAlignment="Top"
Grid.Row="3" />
<Button x:Name="OpenLWD"
Grid.ZIndex="2"
Command="{Binding OpenInLWD3xCommand}"
Margin="0,20,150,0"
HorizontalAlignment="Right"
Width="75"
Height="26"
VerticalAlignment="Top"
Grid.Row="3"
Padding="0"
Background="#26598c">
<Image x:Name="openIn3x"
Source="/Assets/LWD-button.png"
Tag="{Binding JobId}"
Stretch="Fill"
VerticalAlignment="Center"
RenderOptions.BitmapScalingMode="HighQuality"
Height="27">
</Image>
</Button>
<DockPanel Grid.Row="0"
LastChildFill="False"
Grid.RowSpan="4">
<Menu x:Name="menu"
DockPanel.Dock="Top"
IsTabStop="False"
FontSize="13"
VerticalAlignment="Top"
Background="WhiteSmoke"
Height="27">
<MenuItem x:Name="FileMenu"
Header="{x:Static Properties:Resources.File}"
Background="Transparent" FontFamily="Arial">
<MenuItem x:Name="BluetoothUpload"
Header="{x:Static Properties:Resources.UploadBluetoothJob}"
IsEnabled="True"
Command="{Binding OpenLWD3xBluetoothCommand}" />
<MenuItem x:Name="FileUpload"
Header="{x:Static Properties:Resources.UploadLocalFile}"
IsEnabled="True"
Command="{Binding AddLocalJobCommand}" />
<Separator />
<MenuItem x:Name="FileLogout"
Header="{x:Static Properties:Resources.Logout}"
IsEnabled="True"
Command="{Binding LogoutCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
</MenuItem>
<MenuItem x:Name="ViewMenu"
Header="{x:Static Properties:Resources.View}"
Background="Transparent">
<MenuItem x:Name="ColumnOptions"
Header="{x:Static Properties:Resources.ColumnOptions}"
Background="Transparent"
Command="{Binding ShowColumnOptionsDialogCommand}" />
<MenuItem x:Name="DisplayUnits"
Header="{x:Static Properties:Resources.DisplayUnits}"
Background="Transparent"
Command="{Binding ShowUnitsSelectionCommand}" />
<Separator />
<MenuItem x:Name="ViewRefresh"
IsEnabled="True"
Header="Re_fresh"
Command="{Binding RefreshCommand}"
/>
<!--<MenuItem x:Name="ViewOptions" Header="_Options" />-->
</MenuItem>
<MenuItem x:Name="HelpMenu"
Header="{x:Static Properties:Resources.Help}"
Background="Transparent">
<MenuItem x:Name="HelpOpen"
Header="{x:Static Properties:Resources.OnlineHelp}" />
<Separator />
<MenuItem x:Name="HelpAbout"
Header="{x:Static Properties:Resources.About}"
Command="{Binding ShowAboutDialogCommand}" />
<MenuItem x:Name="DeleteUnits"
Header="Delete all Units"
Command="{Binding RemoveUnitsCommand}"/>
</MenuItem>
</Menu>
</DockPanel>
<GridSplitter x:Name="gridSplitter" Grid.Row="2" Width="Auto" MinHeight="5" MaxHeight="5" HorizontalAlignment="Stretch" VerticalAlignment="Center" Margin="0,4,0,4" />
</Grid>
</UserControl>
And here is the JobGridViewStyle:
<Style x:Key="JobGridViewStyle" TargetType="DataGrid">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="Background" Value="WhiteSmoke" />
<Setter Property="SelectionMode" Value="Single" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Margin" Value="10,0,10,0" />
</Style>
I appreciate the help. I'm really not sure why, but the solution to my answer was to add
ShowsPreview="True"
to my GridSplitter. I have no idea why this would affect the functionality, but it fixed my issue.
For reference, I was looking at this page: How to: Resize Rows with a GridSplitter when I found out the solution.
Thanks again

Categories

Resources