"value cannot be null. parameter name current" when trying to drag button - wpf

I have couple of buttons on a WPF Window. When I try to drag them I get an following error in dialog box:
value cannot be null. parameter name current
I do not know how to handle it.
Here is my code:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Style="{DynamicResource MajourColoumnWidth}"/>
<ColumnDefinition Style="{DynamicResource MinourColoumnWidth}"/>
</Grid.ColumnDefinitions>
<Grid Name="ClinicalDetailsGrid1" >
<Grid.RowDefinitions>
<RowDefinition Style="{DynamicResource RowHeight1}"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Content="Save" Name="btnSave" Grid.Row="0" Grid.Column="0"
VerticalAlignment="Center" VerticalContentAlignment="Center"
HorizontalAlignment="Center" Width="100" Height="20"/>
<Button Content="Bill Payment" Name="btnSave1" Grid.Row="0"
Grid.Column="1" VerticalAlignment="Center"
VerticalContentAlignment="Center" HorizontalAlignment="Center"
Width="100" Height="20"/>
</Grid>
</Grid>

Related

WPF How to make control resize relative to next control

I have defined the following XAML:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="200" />
<ColumnDefinition MinWidth="200" />
<ColumnDefinition MinWidth="500" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Margin="2,2,5,2">
<GroupBox Header="Computer">
<DockPanel>
<ComboBox MinWidth="100" Name="cmbComputerNames" IsEditable="True" DockPanel.Dock="Left" HorizontalAlignment="Stretch" Width="auto" />
<Button Content="Connect" Name="bConnect" Width="65" HorizontalAlignment="Right" />
</DockPanel>
</GroupBox>
</StackPanel>
<Button Grid.Column="1" Content="Two" Margin="1,2,5,2" />
<Button Grid.Column="2" Content="Three" Margin="1,2,2,2" />
<GridSplitter Height="100" Width="4" Grid.Column="0"/>
<GridSplitter Height="100" Width="4" Grid.Column="1"/>
</Grid>
So, the left grid column is resizable. I want the "Connect" button to remain right-aligned and with same width. The combobox however, should be left-aligned and the width should grow as the column is resized, so the distance to the connect button remains the same.
Doesn't work:
Can anyone tell me how I can achieve that?
Since this is too long for a comment
Replace DockPanel with Grid and try this:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<GroupBox Header="Some text here">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ComboBox MinWidth="100"/>
<Button Grid.Column="1" Content="A button" Margin="5"/>
</Grid>
</GroupBox>
<GridSplitter Grid.Column="1" Grid.RowSpan="2" ResizeDirection="Columns" ResizeBehavior="PreviousAndNext" Width="10"/>
<Button Content="some button" Grid.Column="2"/>
</Grid>
#Andy, if you could produce an answer then I will delete mine.

Keep Grid center and minimize/maximize on stretching in a WPF Grid

<Grid ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button
VerticalContentAlignment="Stretch"
Margin="0,0,100,0"
Grid.RowSpan="3"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Height="80" Width="30"
Background="blue">
<!--<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>[![enter image description here][1]][1]
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button
x:Name="Add"
Width="25"
Height="25"
Grid.Row="2"
Margin="3,1,3,0"
Padding="0"/>
</Grid>-->
</Button>
<Grid
Margin="0,0,0,100"
Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="3"
VerticalAlignment="Bottom"
HorizontalAlignment="Center"
Width="50" Height="25"
Background="Red">
<Button
x:Name="Add1"
Width="20"
Height="20"
Margin="3,1,3,0"
Padding="0"
HorizontalAlignment="Right"
VerticalAlignment="Top"
VerticalContentAlignment="Center"/>
<Button
x:Name="Remove1"
Width="20"
Height="20"
HorizontalAlignment="Right"
VerticalAlignment="Top"
VerticalContentAlignment="Center"/>
</Grid>
<Grid
Grid.Row="0"
Grid.Column="1"
Grid.RowSpan="3"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="100"
Height="50"
Background="Green">
<!--<Button
x:Name="Add2"
Width="20"
Height="20"
Margin="3,1,3,0"
Padding="0"
HorizontalAlignment="Right"
VerticalAlignment="Top"
VerticalContentAlignment="Center"/>
<Button
x:Name="Remove2"
Width="20"
Height="20"
Margin="3,1,3,0"
Padding="0"
HorizontalAlignment="Right"
VerticalAlignment="Top"
VerticalContentAlignment="Center" />-->
</Grid>
<!--Element #-->
<Grid Margin="0,0,0,100"
Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="3"
VerticalAlignment="Bottom"
HorizontalAlignment="Center"
Width="100" Height="25"
Background="Red">
<!--<Button
Name="myButton"
IsEnabled="False"
Background="#C0C0C0"
BorderThickness="2"
Width="25"
Height="25"/>-->
</Grid>
</Grid>
Is there any possibility to keep all the elements center and stretched to the right when maximize? Every container from Grid can have 3 or 4 children's. Children's can be any container as long as all elements are stretching to right and down but maintained center position.
I attached also the result. All elements are centered but the stretching is missing.
What kind of container is suitable for this situations?
Yes, as o_w mentioned in the comment, a good solution is to use Border. Where you use your child Grids, encapsulate them in a Border and set the Background-property on the Border instead of the Grid. You will see that you will achieve way faster what you want.
The problem you describe is actually something I also struggled with in the beginning, and that's when I found that not only Grid, but the combination of Grid and Borders (with a Grid encapsulated in it) is even more powerful than Grid alone.

remove grid column on shift+space in a wpf application

I have a simple wpf application where I want to remove one column and have the remaining (in column 2) fill the window when ctrl+space is pressed. I've looked into keybinding and commands, but I can't figure this one out.
Code:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="24" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="268" MinWidth="188" MaxWidth="346"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<GridSplitter Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" Height="64" Width="5"/>
<Grid Grid.Row="0" Grid.Column="2"/>
<Label Grid.Row="0" Grid.Column="0" Content="Collections" FontFamily="Noto Sans" Foreground="#FF9B9B9B" FontSize="16" Name="TESTTEST" />
<Button Content="Add" Grid.Row="0" Grid.Column="0" VerticalAlignment="Top" HorizontalAlignment="Right" Grid.ColumnSpan="2" Margin="0,7,3,0" />
</Grid>
<StatusBar Grid.Row="1" Background="#FF151515" Foreground="#FF636363" BorderBrush="#FF1D1D1D"/>
</Grid>

Can we use multi xaml layout on a window?

I think that we can use multi layout on a windows with XAML.
So I try to create a window like this:
I want to create an ListView in left and it can be resized. The Grid and Textbox at the left side will be fit to the right grid.
I have tried to use other layouts, StackPanel, DockPanel.
How can I create a resizable grid
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ToolBar Name="toolBar" Grid.Row="0">
<Button Name="btnLoad" Width="Auto" Height="25" ToolTip= Click="btnLoad_Click" VerticalAlignment="Bottom">
<StackPanel Orientation="Horizontal">
<Image Source="Resources/reload.png" Width="16" Height="16" HorizontalAlignment="Left" Margin="0 0 5 0"/>
<TextBlock>Load/Reload</TextBlock>
</StackPanel>
</Button>
<Button Name="btnSave" Width="Auto" Height="25" Click="btnSave_Click">
<StackPanel Orientation="Horizontal">
<Image Source="Resources/save.png" Width="16" Height="16" HorizontalAlignment="Left" Margin="0 0 5 0"/>
<TextBlock>Save</TextBlock>
</StackPanel>
</Button>
</ToolBar>
<ComboBox Name="cbTypeOfShop" Grid.Row="1" Margin="5 5 5 5"/>
<Grid Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ListView Grid.Column="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
<GridSplitter Grid.Column="1" Grid.RowSpan="3" HorizontalAlignment="Left" VerticalAlignment="Stretch" Background="Black" ShowsPreview="true" Width="5"/>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid Name="dtgListItem" Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
</DataGrid>
<TextBox Grid.Row="1" Height="100" TextWrapping="Wrap" AcceptsReturn="True" Text="1231231231 22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222221231231231231231123123123123123123123123123123123123123123123123123" />
</Grid>
</Grid>
<StatusBar Grid.Row="3" Height="25" HorizontalAlignment="Stretch">
<TextBlock Name="abc">abc</TextBlock>
</StatusBar>
</Grid>
But when I resize the left side.
try this
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Left side</TextBlock>
<GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Stretch" />
<TextBlock Grid.Column="2" FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap">Right side</TextBlock>
</Grid>
As you can see, I've simply created a Grid with two equally wide columns, with a 5 pixel column in the middle.

How to keep image aligned to the right of the window regardless of how the window is resized?

The problem is the image is not fixed to the right side of the screen, so when I resize the screen it the image goes off screen, e.g.:
In that example we should see an image of a full phone.
I have the following Grid layout defined:
<Grid Background="White" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="23" />
<ColumnDefinition Width="166" />
<ColumnDefinition Width="473" />
<ColumnDefinition Width="330" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="35" />
<RowDefinition Height="35" />
<RowDefinition Height="35" />
<RowDefinition Height="50" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- some irrelevant stuff removed -->
<Canvas Grid.Column="3" HorizontalAlignment="Right" >
<Image HorizontalAlignment="Left" Name="imgLogo" Stretch="Uniform" VerticalAlignment="Center" Width="45" Height="45" Margin="30,135,0,0" Canvas.ZIndex="99" Canvas.Left="0" Canvas.Top="-7" />
<Image Source="/Resources/Images/MobileBrandingSample.png" Height="634" Width="316" HorizontalAlignment="Left" Margin="0,14,0,0" Name="imgPhone" Stretch="Uniform" VerticalAlignment="Top" />
<Label Canvas.Left="80" Canvas.Top="127" Content="" Height="20" Name="lblCompanyName" Width="169" FontSize="15" Padding="0,0,0,0" />
<Label Canvas.Left="80" Canvas.Top="150" Height="20" Name="lblPhoneNumber" Width="160" FontSize="12" Padding="0,0,0,0">
<TextBlock Name="tbPhoneNumberLabel" Text="" TextDecorations="Underline" Foreground="#35B6E5" Width="160"></TextBlock>
</Label>
</Canvas>
</Grid>
With this, imgPhone is right aligned, but when I resize the window imgPhone goes off screen. What do I need to keep imgPhone docked to the right of the screen, regardless of how the window is resized?
Ditch the Canvas, use a Dockpanel with a Grid inside it, dock the whole lot right and absolute sizing in the grid will keep things where you want them relative to everything else.
I tried this....and got a solution...maybe not exactly what you want but I think you can work out the few adjustments needed.
<Grid Background="White" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="company name"></TextBlock>
<TextBlock Grid.Row="1" Grid.Column="0" Text="Phone number"></TextBlock>
<TextBlock Grid.Row="2" Grid.Column="0" Text="Logo name"></TextBlock>
<Button Grid.Row="3" Grid.Column="0" Content="Upgrade branding"></Button>
<TextBox Grid.Row="0" Grid.Column="1"></TextBox>
<TextBox Grid.Row="1" Grid.Column="1"></TextBox>
<Button Grid.Row="2" Grid.Column="1" Content="Load logo"></Button>
<Image Grid.Column="2" Grid.RowSpan="4" HorizontalAlignment="Right" Source="C:\Users\somonteiro\Desktop\Montagens\somonteiro.jpg"></Image>
</Grid>

Resources