0

I have a main program window with a row of shortcut buttons along the bottom. I'm trying to make the Visibility of these selectable through a separate Settings window using CheckBoxes, and then store that state in the UserSettings of the program so when it's opened again the previous setting will be remembered. I've found and used an IValueConverter to implement this, and I know the Setting is saved as the CheckBox itself retains it's value.

The problem is that clicking the CheckBox does not affect the Visibility of the button. It works within the Settings window itself, but I can't seem to figure out how to make it work between different windows.

I have tried implementing both the UpdateSourceTrigger & NotifyOnSourceUpdated flags to no avail. Can anybody see the problem?

Main Window XAML

<WrapPanel.Resources>
            <main:BooleanToHiddenVisibility x:Key="boolToVis" />
</WrapPanel.Resources>
<ToggleButton Name="alwaysOnTop" Checked="alwaysOnTop_Checked"
    Style="{StaticResource ShortcutToggleStyle}" Unchecked="alwaysOnTop_Unchecked"
    Visibility="{Binding Source=main:Properties.Settings.Default, Path=pinShow, Converter={StaticResource boolToVis}, Mode=TwoWay}" >
        <Image SnapsToDevicePixels="True" Source="Images/pushpin.png"
             ToolTip="Always on Top" />
</ToggleButton>

Settings Window XAML

<CheckBox Name="pinDisable" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center"
    IsChecked="{Binding Source={x:Static main:Properties.Settings.Default}, Path=pinShow, Mode=TwoWay}"
    Checked="pinDisable_Checked" Unchecked="pinDisable_Unchecked"/>

Convertere Code-Behind

public class BooleanToHiddenVisibility : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Visibility rv = Visibility.Visible;
        try
        {
            var x = bool.Parse(value.ToString());
            if (x)
            {
                rv = Visibility.Visible;
            }
            else
            {
                rv = Visibility.Collapsed;
            }
        }
        catch (Exception)
        {

        }
        return rv;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
}
Keven M
  • 972
  • 17
  • 47
  • I think the big problem is that the property not used implements INotifyPropertyChanged. So you can not tell the components that the configuration has changed. You must create other ways to accomplish this type of change / configuration. – J. Lennon Jul 23 '12 at 14:45
  • Using the immediate window, could you please check if the bindings fail ? i have doubts on the Visibility binding of the ToggleButtons of your MainWindow. – GameAlchemist Jul 23 '12 at 14:58
  • @Vincent Piel When I originally set it up I had it wired to the icons above each checkbox, and they went hidden and visible with each click as expected. It was only when trying to transfer it to the MainWindow that it didn't work. I have an image of each button in one row of a grid, with a corresponding checkbox underneath each image for the user to select. Does this answer your question? – Keven M Jul 23 '12 at 15:13
  • nope :) In the Debug Menu of Visual studio (mine is 2010 Pro) you have 'Window' SubMenu with 'Immediate' as menu. Use this Immediate window to check if any binding error occurs while making your app work (open all windows one after antoher). It should stay empty if all bindings are ok. – GameAlchemist Jul 23 '12 at 15:38
  • (first thing i would do in your case would be to have exact same binding for Visibility of ToggleButton and IsChecked of CheckBox. But i wonder if Properties.Settings does implement PropertyChanged, could not find the answer for you. Try also to put a test CheckBox with its IsChecked set to same binding in the Configuration window itself to see if it changes when the true checkbox changes ) – GameAlchemist Jul 23 '12 at 15:59
  • Tried the Immediate Window, it remained pristinely white. – Keven M Jul 24 '12 at 06:06

0 Answers0