BrownBot Logo BrownBot Head

Fancy Options

8:27 pm Filed under: Uncategorized

With my app running on both sides now (Australia and South Africa), we needed an options control to easily check and edit the various defaults in the app.

Seeing it’s not something people have to look at all day every day I had a bit of a play with the styling to achieve something a bit more out there, it’s totally gratuitous so I didn’t spend a heap of time on it but am pretty happy with the result.

image

Handling application close from a user control in WPF

12:41 am Filed under: Uncategorized

Struck this one the other day and didn’t find a decent answer so here’s my solution.

I’ve got a user control or class that I want to have save one of it’s properties to the Settings.Settings on application shut down.  First thing you’ll find on a user control is that it doesn’t have an OnClose() or OnDispose() to override, lucky for us there’s an Exit event in Appliction.Current that we can subscribe to in our constructor instead.

He’s what it looks like…

public class ScheduleBuilder : INotifyPropertyChanged
{
    public ScheduleBuilder()
    {
        DetailWidth = Properties.Settings.Default.PreProdSchedDetailWidth;

        Application.Current.Exit += new ExitEventHandler(OnApplicationExit);
    }

    private int _detailWidth;

    public int DetailWidth
    {
        get
        {
            return _detailWidth;
        }
        set
        {
            if (value != _detailWidth)
            {
                _detailWidth = value;
                OnPropertyChanged("DetailWidth");
            }
        }
    }

    void OnApplicationExit(object sender, ExitEventArgs e)
    {
        Properties.Settings.Default.PreProdSchedDetailWidth = _detailWidth;
        Properties.Settings.Default.Save();
    }
}

One word of warning, be carefully doing this if you plan you use multiple instances of the class/control, you wont know which one will be that last one to save it’s value.

Bind to the width of a GridSplitter Column WPF

12:27 am Filed under: Uncategorized

Struggled with this one this afternoon, it seems a simple request, bind the width of a Grid Column to a value so it can be saved on program close and shared between Pages in a Navigation control.

The problem is that the width of a ColumnDefinition is not a simple value, it can be Auto, Star or Pixel value, Hence the binding kind of only works one way, I’m sure you could write a value converter to solve this as well but my simple solution is as follows.

XAML

<Grid DockPanel.Dock="Top">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition Width="{Binding Path=DetailWidth, Mode=OneWay}/>
    </Grid.ColumnDefinitions>
    <Grid x:Name="ScheduleGrid" />
    <GridSplitter HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="5" DragCompleted="GridSplitter_DragCompleted"/>
    <Grid Grid.Column="1" />
</Grid>

Code behind

private void GridSplitter_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
    DetailWidth = (int)((sender as GridSplitter).Parent as Grid).ColumnDefinitions[1].Width.Value;
}

The binding in the ColumnDefinition handles the get and we manually do the set in the event handler.

Powered by WordPress