簡體   English   中英

WPF MVVM動態更改靜態資源樣式

[英]WPF MVVM change static resource style dynamically

我是WPF的新手,這是我想要做的。 我想根據是否選中單選按鈕來更改按鈕的樣式,這是一種靜態資源。 下面是示例代碼:

<RadioButton Name="rbInfoCorrect"
             GroupName="InfoQuestion"
             cal:Message.Attach="ShouldProceed">
      <RadioButton.Content>
        <TextBlock Text="Yes" Foreground="White"/>
      </RadioButton.Content>
</RadioButton>
<RadioButton Name="rbInfoNotCorrect"
             GroupName="InfoQuestion"
             cal:Message.Attach="ShouldStop">
      <RadioButton.Content>
        <TextBlock Text="No" Foreground="White"/>
      </RadioButton.Content>
</RadioButton>


<Button x:Name="btnProceed" cal:Message.Attach="Proceed">
  <Button.Style>
    <Style TargetType="Button" BasedOn="{StaticResource WhiteButton}"/>
  </Button.Style>
</Button>
<Button x:Name="btnStop" cal:Message.Attach="Stop">
  <Button.Style>
    <Style TargetType="Button" BasedOn="{StaticResource WhiteButton}"/>
  </Button.Style>
</Button>

因此,當選中“是”時,我想將“繼續”按鈕樣式更改為BlueButton(另一種靜態資源),而當選中“否”時,我想將“停止”按鈕樣式更改為BlueButton。 當然,這是一個更大的問題的一部分,因此,我僅嘗試具體說明示例示例。

我停留的部分是如何根據單選按鈕檢查狀態將StaticResource從WhiteButton更新為BlueButton。 任何方向將不勝感激。

謝謝。

您無需更改整個Style即可。 您只需添加一個簡單的DataTrigger即可直接對Checkbox.IsChecked屬性做出反應。 像這樣聲明Style更加簡單:

<Style x:Key="ProceedButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="White" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsChecked, ElementName=rbInfoCorrect}" 
            Value="True">
            <Setter Property="Background" Value="Blue" />
        </DataTrigger>
    </Style.Triggers>
</Style>

<Style x:Key="StopButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="White" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsChecked, ElementName=rbInfoNotCorrect}" 
            Value="True">
            <Setter Property="Background" Value="Blue" />
        </DataTrigger>
    </Style.Triggers>
</Style>

即使您想更改更多屬性,也可以將默認屬性聲明為Plain Setter ,並由DataTriggerCheckbox.IsChecked屬性觸發的屬性。 如果兩個Style有很多公共屬性,那么仍然可以將它們全部聲明為一個,然后將另一個聲明為基礎,而只需添加不同的DataTrigger

我們可以使用IMultiValue Converter替換整個樣式。

根據您的要求,我想到了這個例子

public class StyleConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType,
       object parameter, CultureInfo culture)
        {
            if (values.Length < 1)
                return Binding.DoNothing;
            bool isCorrect = (bool)values[0];
            bool isNotCorrect = (bool)values[1];
            Style firstStyle = (Style)values[2];
            Style secondStyle = (Style)values[3];
            if (isCorrect)
                return firstStyle;
            if (isNotCorrect)
                return secondStyle;
            return Binding.DoNothing;

        }

        public object[] ConvertBack(object value, Type[] targetTypes,
            object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

XAML

<Window x:Class="StackWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:StackWpf"
        Title="MainWindow" Name="window" Height="350" Width="525"  >
    <Window.Resources>

        <ResourceDictionary>
            <Style TargetType="Button" x:Key="WhiteStyle">
                <Setter Property="Background" Value="White"/>
            </Style>
            <Style TargetType="Button" x:Key="BlueStyle">
                <Setter Property="Background" Value="Blue"/>
            </Style>
            <local:StyleConverter x:Key="styleConverter"/>
        </ResourceDictionary>
    </Window.Resources>

    <Grid>
        <RadioButton Name="rbInfoCorrect" IsChecked="False"
             GroupName="InfoQuestion" Margin="80,19,382,257">
            <RadioButton.Content>
                <TextBlock Text="Yes" Foreground="Black"/>
            </RadioButton.Content>
        </RadioButton>
        <RadioButton Name="rbInfoNotCorrect" IsChecked="False"
             GroupName="InfoQuestion" Margin="80,38,391,257">
            <RadioButton.Content>
                <TextBlock Text="No" Foreground="Black"/>
            </RadioButton.Content>
        </RadioButton>
        <Button Content="Button" Margin="80,114,294,161">
            <Button.Style>


                        <MultiBinding Converter="{StaticResource styleConverter}">
                            <Binding ElementName="rbInfoCorrect"
                             Path="IsChecked" />
                            <Binding ElementName="rbInfoNotCorrect"
                             Path="IsChecked" />
                            <Binding Source="{StaticResource WhiteStyle}" />
                            <Binding Source="{StaticResource BlueStyle}" />
                        </MultiBinding>


            </Button.Style>
        </Button>

    </Grid>
</Window>

我已經使用了這篇好文章http://social.msdn.microsoft.com/Forums/vstudio/en-US/b25973bb-9e9c-4a99-8234-39a042e0a478/apply-styles-dynamically-to-buttons-in-xaml嗎? forum = wpf可以解決我的問題。

我不確定是否可以替換完整的Style,但是可以使用DataTrigger更改按鈕的某些屬性:

    <Button.Style>
      <Style TargetType="Button" BasedOn="{StaticResource WhiteButton}">
        <Style.Triggers>
          <DataTrigger Binding="{Binding ElementName=rbInfoCorrect, Path=IsChecked}" Value="True">
            <Setter Property="Background" Value="White" />
          </DataTrigger>
          <DataTrigger Binding="{Binding ElementName=rbInfoCorrect, Path=IsChecked}" Value="False">
            <Setter Property="Background" Value="Blue" />
          </DataTrigger>
        </Style.Triggers>
      </Style>
    </Button.Style>

查看http://Globalizer.codeplex.com

切換語言時,它將切換整個樣式。

您基本上可以在資源字典中查找樣式,將其殺死並加載一個新樣式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM