I want to show a UserControl in MainWindow's Grid, but not working when double click it won't show the UserControl.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" MaxWidth="400" MinWidth="0"/>
<ColumnDefinition Width="0"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Margin="0,0,0,0">
<TreeView Background="Transparent">
<TreeViewItem Header="Purchase" IsExpanded="False">
<TreeViewItem Header="Material" x:Name="TreeViewItem_Material" MouseDoubleClick="TreeViewItem_Material_MouseDoubleClick"></TreeViewItem>
</TreeViewItem>
</TreeView>
</Grid>
<GridSplitter Margin="0,0" Width="5"/>
<Grid Grid.Column="2" x:Name="MainGrid" Margin="0,0,0,0"></Grid>
</Grid>
private void TreeViewItem_Material_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
UserControl material = new Material(); //UserControl: Material.xaml
MainGrid.Children.Add(material);
}
Thank you.
Edit-----
My code running well, just UserControl Layout problem that's cause not showing.
Your code works well in my environment as shown below.
The Hello, Material shows when double-click node Material.
Check Material visibility or use a simple UserControl may helps.
Related
I have a ListView that lays on top of UI graphics in my UserControl that I want to show and size using a Grid that matches the parent grid. I'm trying to template the way each item shown and sized using the ListView.ItemTemplate. The issue I'm having is that the DataTemplate is not sizing to the main Grid of the UI, and I'm not sure why.
This is what I've done, I think it a fairly simple and straight forward datatemplate:
<ListView ItemsSource="{Binding Path=StatusFilter}" Grid.Row="2" Grid.ColumnSpan="12">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="92"/>
<ColumnDefinition Width="90"/>
<ColumnDefinition Width="85"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
<ColumnDefinition Width="90*"/>
</Grid.ColumnDefinitions>
<Label Content="{Binding Path=ECNNumber}" Grid.Column="0"/>
<Label Content="{Binding Path=DateECNDue}" Grid.Column="1"/>
<Label Content="{Binding Path=Model.Model}" Grid.Column="2"/>
<--More stuff for each column-->
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
When this is displayed the grid is completely bypassed and each element is shown like a horizontally aligned stackpanel. To test that my DataTemplate wasn't faulty I threw it into an ItemsControl and it worked perfectly. It resized with the parent grid without any issues.
Why isn't this working for the ListView, and how do I get it to work? I need the selection and scrollview capability of the ListView otherwise I'd be using the ItemsControl.
I read something about Grid.SharedSizeScope but that doesn't work with dynamic sizing of grid, and that's all I could really find on this issue.
With the XAML below when I drag the GridSplitter to the left it pushes elements out of the window.
How can I keep all elements within the window frame?
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Content="0" />
<Button Grid.Column="1" Content="1" />
<Button Grid.Column="2" Content="2" />
<GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Left" />
</Grid>
Thanks
The only way I know to solve your problem is have the columns that are left and right of your gridsplitter have the width property set as Width="*" and give the GridSplitter its own column with a HorizontalAlignment set as HorizontalAlignment="Stretch". Your code would then end up looking like this.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Content="0" />
<GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Stretch"/>
<Button Grid.Column="2" Content="1" />
<Button Grid.Column="3" Content="2" />
</Grid>
I was having this same issue, and came up with this solution. Basically, the idea is to dynamically change the MaxWidth of the appropriate column when the grid/columns change. I've shown with two columns here, but I've used this approach with three columns successfully.
With this approach, if you resize the window down to not fit the contents of the grid anymore, the grid stops changing size, so the ResizeGrid_SizeChanged event stops getting called. To solve for this, you can also listen for the window (or user control) size change events. You may also need to bind the MaxWidth of your grid appropriately, or use the control size directly if your grid fills the window/UserControl. I've shown how to bind the MaxWidth property through XAML here. If you didn't want to do that, you could replace "ResizeGrid.MaxWidth" with "ActualWidth" inside the window code behind, and remove the "MaxWidth" binding on the "ResizeGrid" object.
XAML:
<Window x:Class="App.Window1"
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:Default="clr-namespace:App"
mc:Ignorable="d"
SizeChanged="Window_SizeChanged"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid x:Name="ResizeGrid" SizeChanged="ResizeGrid_SizeChanged"
MaxWidth="{Binding ActualWidth, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Default:Window1}}}">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="C0" Width="150" MinWidth="50" />
<ColumnDefinition Width="5" />
<ColumnDefinition x:Name="C2" Width="*" MinWidth="50" />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Background="Green">
<Label Content="Left" />
<Label Content="Right" HorizontalAlignment="Right" />
</Grid>
<GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Stretch" DragCompleted="GridSplitter_DragCompleted" />
<Grid Grid.Column="2" Background="Red">
<Label Content="Left" />
<Label Content="Right" HorizontalAlignment="Right" />
</Grid>
</Grid>
</Grid>
</Window>
C# Code Behind
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
UpdateGridSplitterWidths();
}
private void ResizeGrid_SizeChanged(object sender, SizeChangedEventArgs e)
{
UpdateGridSplitterWidths();
}
private void GridSplitter_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
UpdateGridSplitterWidths();
}
private void UpdateGridSplitterWidths()
{
C0.MaxWidth = Math.Min(ResizeGrid.ActualWidth, ResizeGrid.MaxWidth) - (C2.MinWidth + 5);
}
Just to add to #Joseph's answer, which pointed me in the right direction, it still works when you fraction the "*", or include min/max widths.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.35*"
MinWidth="48" />
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"
MaxWidth="48" />
</Grid.ColumnDefinitions>
This lets you set the initial width of each column in keeping with your intent, as well as providing the functionality of not pushing things off screen.
How can I get a handle for the GridSplitter inside the TitleTemplate of RadPane
<telerik:RadPane TitleTemplate="{StaticResource radPaneHeaderFormat}" ContextMenuTemplate="{x:Null}" CanDockInDocumentHost="False" x:Name="radPane" Header="HeaderTitle" CanUserClose="False" CanFloat="False" VerticalAlignment="Center" />
<DataTemplate x:Key="radPaneHeaderFormat}" ">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="670" MaxWidth="678"/>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="0.1*" />
</Grid.ColumnDefinitions>
<Button x:Name="ViewButton" Margin="0,0,5,0" Style="{StaticResource LeftArrowButtonStyle}" Click="ViewButton_Click" />
<TextBlock Text="Summary" VerticalAlignment="Center" Grid.Column="1" />
<sdk:GridSplitter Height="Auto" Grid.Column="2" Name="HeaderSplitter" VerticalAlignment="Stretch" Style="{StaticResource newGridSplitterStyle}" />
<TextBlock Text="Note" Grid.Column="3" VerticalAlignment="Center" Margin="3,0,0,0"/>
</Grid>
</DataTemplate>
Can any one help me with this issue.
I don't know from where you want to access the GridSplitter, but, given that you have a Button with a Click event handler, I'll show you how you can access it in there:
private void ViewButton_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
var panel = button.Parent as Panel;
if (panel != null)
{
GridSplitter splitter = panel.Children.OfType<GridSplitter>().First();
// do stuff with the GridSplitter...
}
}
You will need to add a line using System.Linq; if you don't already have one.
In this case I'm making use of the fact that the button's Click event handler passes us the button that was clicked, and that the Button and the GridSplitter have the same parent.
I'm trying to build a two-column layout where the width of the columns can be changed by using a splitter. The right column's width shouldn't change when the browser window is resized (it shouldn't be proportional to the grid width). Both columns should have a minimum width. When the browser window is too narrow to display both columns a scrollbar should appear.
This is my XAML:
<Grid x:Name="LayoutRoot" Background="White">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
<Grid Height="200" Margin="0,0,0,0" MinWidth="400" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="300" Width="300*" />
<ColumnDefinition Width="10" />
<ColumnDefinition MinWidth="100" Width="100"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Background="Red" x:Name="LeftColumn"></Grid>
<sdk:GridSplitter Grid.Column="1" HorizontalAlignment="Center" Width="10" Background="Black" />
<Grid Grid.Column="2" Background="Green" x:Name="RightColumn"></Grid>
</Grid>
</ScrollViewer>
</Grid>
The problem I'm having is when the splitter is dragged to the left and the left column's minwidth is reached - the right column begins to grow very rapidly and the scrollbar appears. Removing the width setting from the right column eliminates the weird behavior but now the right column starts to grow proportionally when the window is resized...
I'd like the splitter to behave the same way as when it is dragged to the right - I'd like it to stop after the minwidth is reached.
You should disable the "HorizontalScrollBarVisibility".
This code works for me:
<Grid x:Name="LayoutRoot" Background="White">
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled">
<Grid Height="200" Margin="0,0,0,0" MinWidth="400" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="300" Width="300*" />
<ColumnDefinition Width="10" />
<ColumnDefinition MinWidth="100" Width="100"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Background="Red" x:Name="LeftColumn"></Grid>
<sdk:GridSplitter Grid.Column="1" HorizontalAlignment="Center" Width="10" Background="Black" />
<Grid Grid.Column="2" Background="Green" x:Name="RightColumn"></Grid>
</Grid>
</ScrollViewer>
</Grid>
The ScrollViewer gives the grid endless space to grow. Hence the minWidth never stops it.
Obviously there is no need of ScrollViewer that is disabled both vertically and horizontally. Better move the scroll bar inside the grid surrounding the content of every column.
I think I was finally able to come up with a workaround. I'm forcing the width in the code-behind when the layout is changing.
XAML:
<Grid x:Name="LayoutRoot" Background="White">
<ScrollViewer x:Name="Scroller" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
<Grid x:Name="Workspace" Height="200" Margin="0,0,0,0" MinWidth="400" VerticalAlignment="Top" LayoutUpdated="Workspace_LayoutUpdated">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="300" Width="300*" />
<ColumnDefinition Width="10" />
<ColumnDefinition MinWidth="100" Width="100"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Background="Red" x:Name="LeftColumn"></Grid>
<sdk:GridSplitter Grid.Column="1" HorizontalAlignment="Center" Width="10" Background="Black" />
<Grid Grid.Column="2" Background="Green" x:Name="RightColumn"></Grid>
</Grid>
</ScrollViewer>
</Grid>
Code behind:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void Workspace_LayoutUpdated(object sender, EventArgs e)
{
Workspace.Width = Scroller.ViewportWidth - 1;
}
}
The reason it behaves this way is that you have specified the first column Width="300*" with asterisks,
and the third column Width="100" without asterisks.
Just put asterisks to the first and third columns, or remove respectively, and it will work the way you wish.
I have the following (simplified) xaml:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="143*" />
<ColumnDefinition Width="135*" />
</Grid.ColumnDefinitions>
<TabControl Margin="12,29" Name="tabControl1" Grid.ColumnSpan="2">
<TabItem Header="TabItem1" Name="tabItem1">
<ScrollViewer Height="440" Name="scrollViewer1" Width="872" HorizontalAlignment="Left" VerticalAlignment="Top">
<Grid Height="440" Name="grid1" Width="851" VerticalAlignment="Top" HorizontalAlignment="Left">
...
My problem is the scroll won't show up. What should I change ?
Thanks.
The content inside the ScrollViewer is smaller than the ScrollViewer itself. The ScrollViewer is used for being able to use scrollbars to show content that is larger than the area the ScrollViewer takes up.