ToggleSwitch - 如何启用文本?

时间:2010-11-28 19:44:00

标签: windows-phone-7 toggle

我正在使用WP7控件工具包中的两个toggleSwitches。基于第一个切换,应启用或禁用第二个切换开关。第二个切换开关的禁用工作正常但是当执行启用时,文本前景永远不会改变。请帮我弄清楚为什么会这样。

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <toolkit:ToggleSwitch Header="twitter" Margin="10,15,0,0" Name="toggleTwitter" Checked="toggleTwitter_Checked" Unchecked="toggleTwitter_Unchecked">
        <toolkit:ToggleSwitch.HeaderTemplate>
            <DataTemplate>
                <ContentControl FontSize="{StaticResource PhoneFontSizeLarge}" Foreground="{StaticResource PhoneForegroundBrush}" Content="{Binding}"/>
            </DataTemplate>
        </toolkit:ToggleSwitch.HeaderTemplate>
        <toolkit:ToggleSwitch.ContentTemplate>
            <DataTemplate>
                <StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Status: " FontSize="{StaticResource PhoneFontSizeMedium}"/>
                        <ContentControl HorizontalAlignment="Left" FontSize="{StaticResource PhoneFontSizeMedium}" Content="{Binding}"/>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </toolkit:ToggleSwitch.ContentTemplate>
    </toolkit:ToggleSwitch>
    <toolkit:ToggleSwitch Header="" Margin="10,100,0,-35" Name="toggleTwitterAutoPublish" Checked="toggleTwitterAutoPublish_Checked" Unchecked="toggleTwitterAutoPublish_Unchecked">
        <toolkit:ToggleSwitch.ContentTemplate>
            <DataTemplate>
                <StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Auto Publish: " FontSize="{StaticResource PhoneFontSizeMedium}" Margin="0,-15,0,0" />
                        <ContentControl HorizontalAlignment="Left" FontSize="{StaticResource PhoneFontSizeMedium}" Content="{Binding}" IsEnabled="{Binding}" Margin="0,-15,0,0"/>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </toolkit:ToggleSwitch.ContentTemplate>
    </toolkit:ToggleSwitch>
</Grid>


public partial class MainPage : PhoneApplicationPage
{
    bool isConnected = false;
    bool isAutoPublish = false;

    public const string SIGNED_IN_MESSAGE = "Signed In";
    public const string SIGNED_OUT_MESSAGE = "Signed Out";
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        toggleTwitter.IsChecked = isConnected;

        AlterTwitterControlsDisplay();

        base.OnNavigatedTo(e);
    }


    #region Twitter

    private void AlterTwitterControlsDisplay()
    {
        if (toggleTwitter.IsChecked.Value)
        {
            toggleTwitter.Content = SIGNED_IN_MESSAGE;
            toggleTwitterAutoPublish.IsEnabled = true;
            toggleTwitterAutoPublish.IsChecked = isAutoPublish;
        }
        else
        {
            toggleTwitter.Content = SIGNED_OUT_MESSAGE;
            toggleTwitterAutoPublish.IsEnabled = false;
            toggleTwitterAutoPublish.IsChecked = false;
        }
    }

    private void toggleTwitter_Checked(object sender, RoutedEventArgs e)
    {
        isConnected = true;
        AlterTwitterControlsDisplay();
    }

    private void toggleTwitter_Unchecked(object sender, RoutedEventArgs e)
    {
        isConnected = false;
        AlterTwitterControlsDisplay();
    }

    private void toggleTwitterAutoPublish_Checked(object sender, RoutedEventArgs e)
    {
        isAutoPublish = true;
    }

    private void toggleTwitterAutoPublish_Unchecked(object sender, RoutedEventArgs e)
    {
        isAutoPublish = false;
    }

    #endregion Twitter
}

1 个答案:

答案 0 :(得分:1)

在做

toggleTwitterAutoPublish.IsChecked = false; 

(在AlterTwitterControlsDisplay函数的其他部分),toggleTwitterAutoPublish_Unchecked被触发,设置isAutoPublish = false

因此下次当你尝试

toggleTwitterAutoPublish.IsChecked = isAutoPublish;

此处isAutoPublishfalse,因此您可能无法获得所需的结果。

这是我从你的问题中理解的。如果这不是问题,请清楚解释。希望这有帮助