StarLordBlair
StarLordBlair

Reputation: 583

Dynamically change the width of a WPF window when content gets visible

I want to be able to hide the left hand side of the screen and right hand side of the screen at the beginning of the program.

Then when the user presses create new button the left hand side of the screen becomes available so they can create the new item. Then when they press save it comes back to the middle datagrid only.

Then I want to add an event when the double click on the datagrid row (data is programmed in to the datagrid in the code) the right hand side of the screen becomes visible then when the button allocate is pressed the right hand side disappears again just leaving the datagrid.

I am fairly new to WPF so unsure whether this can be done or not. I am trying to do it in the same window at the moment I am making prototypes for my company and already have some that use separate windows. I had posted an image but am un able to post it as I am a new user.

Upvotes: 0

Views: 2813

Answers (1)

Dan Busha
Dan Busha

Reputation: 3803

To hide and show controls I would either recommend using expanders (as your comment says you've done) or Grids and setting their visibility as needed. If you want your side panels to appear over the datagrid then displaying them is just a matter of changing their visibility. If don't want obscure the DataGrid then you will need to change the visibility of the panels as well as the size of the window.

XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow">
<Grid>
    <!-- DataGrid display -->
    <Grid>
        <StackPanel>
            <Button Content="Add New" Click="OnAddNewButtonClick" Width="100"/>
            <DataGrid ItemsSource="{Binding GridItems}" IsReadOnly="True" Name="dataGrid">
                <DataGrid.RowStyle>
                    <Style TargetType="{x:Type DataGridRow}">
                        <EventSetter Event="MouseDoubleClick" Handler="OnRowDoubleClick"/>
                    </Style>
                </DataGrid.RowStyle>
            </DataGrid>
        </StackPanel>
    </Grid>

    <!-- Left column pops up over DataGrid -->
    <Grid Name="LeftColumn" Visibility="Collapsed" Background="Red" Width="200" HorizontalAlignment="Left">
        <StackPanel  VerticalAlignment="Center">
            <Button Content="Hide Column" Click="OnLeftColumnButtonClick"/>
        </StackPanel>
    </Grid>

    <!-- Right Column expands screen size-->
    <Grid  Visibility="Collapsed" Name="RightColumn" Width="200" HorizontalAlignment="Right">
        <StackPanel Background="Green" >
            <TextBlock Text="Hidden Column"/>
            <Button Content="Hide Panel" Click="OnRightColumnButtonClick"/>
        </StackPanel>
    </Grid>
</Grid>
</Window

C# - I know you're working in VB, but this was quicker for me. The code should be fairly self explanatory, but if you need a VB sample let me know:

public partial class MainWindow : Window
{
    public ObservableCollection<Person> GridItems { get; set; }
    private const double CollapsedWidth =  500;
    private const double ExpandedWidth = 700;

    public MainWindow()
    {
        DataContext = this;
        GridItems = new ObservableCollection<Person>();
        GridItems.Add(new Person { Name = "Foo", Age = 1 });
        GridItems.Add(new Person { Name = "Bar", Age = 2 });
        InitializeComponent();
        Width = CollapsedWidth;

    }

    private void OnAddNewButtonClick(object sender, RoutedEventArgs e)
    {
        LeftColumn.Visibility = Visibility.Visible;
    }

    private void OnLeftColumnButtonClick(object sender, RoutedEventArgs e)
    {
        LeftColumn.Visibility = Visibility.Collapsed;
    }

    private void OnRowDoubleClick(object sender, MouseButtonEventArgs e)
    {
        Width = ExpandedWidth;
        RightColumn.Visibility = Visibility.Visible;
    }

    private void OnRightColumnButtonClick(object sender, RoutedEventArgs e)
    {
        RightColumn.Visibility = Visibility.Collapsed;
        Width = CollapsedWidth;
    }
}

Upvotes: 1

Related Questions