View local:"UserControl" in ColumnDefinition(2) - wpf

I want to view UserControl inside ColumnDefinition(2)
MainWindow.xaml:
<Grid Margin="0,0,0,0">
<local:DockPanelTop />
<local:DockPanelBtm />
<Grid Margin="5,45,5,25" x:Name="MainGrid" x:FieldModifier="public">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" MaxWidth="400" MinWidth="0" x:Name="MainMenu" x:FieldModifier="public" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Rectangle Grid.Column="0" Fill="AliceBlue" />
<GridSplitter HorizontalAlignment="Stretch" Background="#FFFF7E7E" Grid.Column="1" />
<Rectangle Grid.Column="2" Fill="AntiqueWhite" />
【"View this into ColumnDefinition(2)"<local:TreeView />】
</Grid>
Thanks.

My Answer:
<local:TreeView Grid.Column="2"/>

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.

Toggle button and vertical grid splitter is not working simultaneously

This is my sample code ,Please help me to achieve both goals simultaneouslyImage
On click toggle button collapse and visible column and vertical split button.
In the below fig. First add toggle button and First column contain two column .
It contains second sub column is collapse or disable based on toggle button click.
and Spltter is working on outside two main column please help me as soon as possible
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="5"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40" />
<ColumnDefinition Width="300"/>
</Grid.ColumnDefinitions>
<Border Background="Green"
Grid.Column="0">
<Grid Grid.Column="1"
Visibility="{Binding IsChecked, ElementName=toggleButton, Converter={StaticResource BooleanToVisibilityConverter}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="300" />
</Grid.ColumnDefinitions>
<WrapPanel Grid.Column="1"
Background="Aqua" />
</Grid>
</Border>
<ToggleButton x:Name="toggleButton"
Width="30"
Height="30"
Margin="0,10,10,0"
IsChecked="True"
HorizontalAlignment="Left"
VerticalAlignment="Top" />
</Grid>
<GridSplitter Width="5"
Grid.Column="1"
ResizeBehavior="CurrentAndNext" />
<Grid Grid.Column="2"></Grid>
</Grid>
From reading your question, I believe this is what you are trying to achieve. Please let me know if I didn't understand you properly.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="5" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Border
Grid.Column="0"
Background="Green">
<ToggleButton x:Name="toggleButton"
Width="30"
Height="30"
Margin="0,10,10,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
IsChecked="True" />
</Border>
<Grid
Grid.Column="1"
MaxWidth="300"
Visibility="{Binding ElementName=toggleButton, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}">
<WrapPanel Background="Aqua">
<TextBlock
Margin="8"
Text="Item 01" />
<TextBlock
Margin="8"
Text="Item 02" />
<TextBlock
Margin="8"
Text="Item 03" />
<TextBlock
Margin="8"
Text="Item 04" />
<TextBlock
Margin="8"
Text="Item 05" />
</WrapPanel>
</Grid>
<GridSplitter
Grid.Column="1"
Width="5"
Visibility="{Binding ElementName=toggleButton, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}" />
<Grid Grid.Column="3">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="Column 2" />
</Grid>
</Grid>

Sharing columns or rows in a grid

I'm trying to create an asymmetrical layout using a Grid where i have 2 rows, 2 columns and an extra shared column as follows:
<Grid Background="Black">
<Grid.RowDefinitions>
<RowDefinition Height="200" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Rectangle
Grid.Row="0"
Grid.Column="0"
Width="200"
Height="200"
Fill="Red" />
<Rectangle
Grid.Row="0"
Grid.Column="1"
Grid.ColumnSpan="2"
Width="250"
Height="200"
Fill="Blue" />
<Rectangle
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2"
Width="250"
Height="200"
Fill="Yellow" />
<Rectangle
Grid.Row="1"
Grid.Column="2"
Width="200"
Height="200"
Fill="Green" />
</Grid>
But however i try to set it up, the second column always collapses unless i explicitly set a fixed width (in this case 50px). Why is this happening?
Shouldn't the second column resize itself to the remainder of each rectangle?
Using a Converter i managed to manually calculate the shared column size by placing the content of the first column in a container and binding the width of the shared column to the ActualWidth of that container subtracted by the first column width.
e.g.
<Grid Background="Black">
<Grid.RowDefinitions>
<RowDefinition Height="200" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
<ColumnDefinition Width="{Binding ElementName=Rect3, Path=ActualWidth, Converter={StaticResource SharedColumnConverter}, ConverterParameter=200}" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Rectangle
Grid.Row="0"
Grid.Column="0"
Width="200"
Height="200"
Fill="Red" />
<Rectangle
Grid.Row="0"
Grid.Column="1"
Grid.ColumnSpan="2"
Width="250"
Height="200"
Fill="Blue" />
<Rectangle
Name="Rect3"
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2"
Width="250"
Height="200"
Fill="Yellow" />
<Rectangle
Grid.Row="1"
Grid.Column="2"
Width="200"
Height="200"
Fill="Green" />
</Grid>

WPF fit row to grid width with auto sized columns

I have a problem with fitting grid row to parent grid width. So the code is (table header):
<Grid Height="Auto" Margin="20" VerticalAlignment="Top" >
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="0" >Word</Label>
<Label Grid.Column="1" Grid.Row="0" >Class</Label>
<Label Grid.Column="2" Grid.Row="0" >Match case</Label>
<Label Grid.Column="3" Grid.Row="0" >Regular expression</Label>
<Label Grid.Column="4" Grid.Row="0" >Commands</Label>
</Grid>
The problem is: if all columns based on Width="Auto" the resulting rows will not fit parent grid. I know that I can fix this by setting Width="*" on one of the columns, but it's not what I want.
I need auto size on all columns with fitting to grid width (which can be much bigger then calculated auto-size of a row). How can I archive that?
Use Viewbox if you don't want to set Width="Auto", it is properly fitting the grid.
<Viewbox Stretch="Uniform" VerticalAlignment="Top">
<Grid Height="Auto" Margin="20">
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="0" >Word</Label>
<Label Grid.Column="1" Grid.Row="0" >Class</Label>
<Label Grid.Column="2" Grid.Row="0" >Match case</Label>
<Label Grid.Column="3" Grid.Row="0" >Regular expression</Label>
<Label Grid.Column="4" Grid.Row="0" >Commands</Label>
</Grid>
</Viewbox>
You could use the SharedSizeGroup property to share sizing properties between several different ColumnDefinitions that belongs to different Grids: https://msdn.microsoft.com/en-us/library/system.windows.controls.definitionbase.sharedsizegroup(v=vs.110).aspx
<Grid Height="Auto" Margin="20" VerticalAlignment="Top" >
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="a" />
<ColumnDefinition SharedSizeGroup="b" />
<ColumnDefinition SharedSizeGroup="c" />
<ColumnDefinition SharedSizeGroup="d" />
<ColumnDefinition SharedSizeGroup="e" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="0" >Word</Label>
<Label Grid.Column="1" Grid.Row="0" >Class</Label>
<Label Grid.Column="2" Grid.Row="0" >Match case</Label>
<Label Grid.Column="3" Grid.Row="0" >Regular expression</Label>
<Label Grid.Column="4" Grid.Row="0" >Commands</Label>
</Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="a" />
<ColumnDefinition SharedSizeGroup="b" />
<ColumnDefinition SharedSizeGroup="c" />
<ColumnDefinition SharedSizeGroup="d" />
<ColumnDefinition SharedSizeGroup="e" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" TextWrapping="Wrap">Some long word.....</TextBlock>
<!-- -->
</Grid>

Can we move Grid Splitter On Left like we drag it on button click in wpf?

<Grid>
<StackPanel>
<StackPanel Orientation="Horizontal">
<Button Content="◄" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Button Content="►" HorizontalAlignment="Right" VerticalAlignment="Top"/>
</StackPanel>
<Grid Height="245">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!--Declaring Label-->
<DockPanel LastChildFill="False" Background="Yellow">
<Label Content="Left" Grid.Column="0" DockPanel.Dock="Left" />
</DockPanel>
<!--Splitter Code-->
<GridSplitter HorizontalAlignment="Right" VerticalAlignment="Stretch" Grid.Column="1" ResizeDirection="Columns"
Background="Black" ResizeBehavior="PreviousAndNext" Width="10"/>
<!--Declaring Label-->
<Label Content="Right" Grid.Column="2" Background="Pink"/>
</Grid>
</StackPanel>
</Grid>
I want to drag Grid Splitter on button Click. I tried Double Animation.StoryBoard which works on grid but not grid splitter.
[]1
You have to resize grid columns, not gridsplitter.
private void BtnLeft_Click(object sender, RoutedEventArgs e)
{
MyGrid.ColumnDefinitions[0].Width = new GridLength(100);
}
<Grid x:Name="MyGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Content="Left" Background="LightBlue" />
<GridSplitter Grid.Column="1" Width="10" ResizeBehavior="PreviousAndNext" />
<Label Content="Right" Background="LightGoldenrodYellow" Grid.Column="2" />
<Button Content="Left" HorizontalAlignment="Left" VerticalAlignment="Top" Click="BtnLeft_Click" />
</Grid>

Resources