0

我已经设法根据这里的代码将按钮样式保存到我的 AppSetting 类中无法使用 Image 和 Name 正确填充 ListPicker 控件

然后我想绑定按钮的正常和按下的背景图像(我有几个按钮样式,用户可以在设置页面中选择)。

    <Style x:Key="ButtonStyle" TargetType="Button">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Disabled"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Normal">
                                    <Storyboard>

                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="NormalBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="PressedBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Collapsed</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>

                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="NormalBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Collapsed</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="PressedBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="ButtonBackground" BorderBrush="{x:Null}">

                            <Grid>
                                <Image x:Name="NormalBackground" Source="{Binding ButtonUpSetting, Source={StaticResource AppSettings}}" Stretch="Uniform"/>
                                <Image x:Name="PressedBackground" Source="{Binding ButtonDownSetting, Source={StaticResource AppSettings}}" Stretch="Uniform"/>

                                <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Grid>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

特别是我想将以下项目绑定到它们各自的 uri 源,但不能执行“实时”绑定。

 <Image x:Name="NormalBackground" Source="{Binding ButtonUpSetting, Source={StaticResource AppSettings}}" Stretch="Uniform"/>
 <Image x:Name="PressedBackground" Source="{Binding ButtonDownSetting, Source={StaticResource AppSettings}}" Stretch="Uniform"/>

我也在INotifyPropertyChanged我的 AppSetting 类中实现了,但是当我返回按钮页面时,按钮样式没有更新。

更新:

该按钮仅在我关闭并重新打开应用程序时更新。因此它可以从设置中读取值。但是在我更改设置后它不会立即更新。

页面结构如下:

ButtonPage <> SettingPage <> AppSetting 类

那么,在 SettingPage 中更改它们后,如何确保 ButtonPage “重新读取”​​ AppSetting 类中的设置?我不确定是否INotifyPropertyChanged会通知 ButtonPage,因为它在我对 SettingPage 进行更改时位于后台堆栈中。

在 ButtonPage OnNavigatedTo 事件中设置 DataContext = AppSetting 并没有什么不同。

4

1 回答 1

1

我已经想通了。

仅在代码中定义 Datacontext 而不是在 XAML 中,所以

<Image x:Name="NormalBackground" Source="{Binding ButtonUpSetting}" Stretch="Uniform"/>
<Image x:Name="PressedBackground" Source="{Binding ButtonDownSetting}" Stretch="Uniform"/>

然后

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        base.OnNavigatedTo(e);

        //Rebind all bindings to update button background images
        if (e.NavigationMode == NavigationMode.Back)
        {
            this.DataContext = null;
            this.DataContext = settings;
        }
        ....
于 2013-02-16T05:30:20.277 回答