绑定按钮' WP7 中 ViewModel 属性的样式

发布于 2024-11-30 13:43:22 字数 508 浏览 1 评论 0原文

我在音频记录视图中有一个播放按钮。

目前它被定义为:

<Button Width="72" Height="72" Style="{StaticResource RoundPlay}" 
                DataContext="{Binding ElementName=this, Path=DataContext}"
                cmd:ButtonBaseExtensions.Command="{Binding PlayStopCommand}"
                />

当用户单击按钮时,项目 ViewModel 中的 PlayStopCommand 被执行。我希望每当声音播放时按钮的样式设置为“RoundStop”。

如何将按钮的样式绑定到 ViewModel 中的属性(我应该使用什么属性类型),以便可以通过代码控制按钮的外观?

我定义了 RoundStop 样式,我只需要一种将其应用到代码中的按钮的方法。

I have a play button in a AudioRecord View.

Currently it is declered as:

<Button Width="72" Height="72" Style="{StaticResource RoundPlay}" 
                DataContext="{Binding ElementName=this, Path=DataContext}"
                cmd:ButtonBaseExtensions.Command="{Binding PlayStopCommand}"
                />

When a user clicks the button, a PlayStopCommand in items ViewModel gets executed. I want the button to get its' style set to "RoundStop" whenever the sound is playing.

How can I bind the buttons' Style to a property in my ViewModel (what property type should I use), so that the look of the button is controllable from code?

I have RoundStop style defined, I just need a way to apply it to a button from code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜你已久 2024-12-07 13:43:22

您应该在视图模型中定义播放状态(播放/停止),并使用转换器将 Button.Style 绑定到该属性。在转换器中,根据当前状态返回不同的样式(取自 App.Current.Resources)。

编辑:

以下是转换器的示例,应如下所示:

public class StateStyleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (PlaybackState)value == PlaybackState.Playing ? App.Current.Resources["RoundPlay"] : App.Current.Resources["RoundStop"];
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

在此示例中,PlaybackState 是一个枚举:

public enum PlaybackState
{
    Playing,
    Stopped
}

然后您应该将状态属性添加到视图模型中(通知更改的部分取决于用于 MVVM 的框架):

private PlaybackState state;
public PlaybackState State
{
    get { return state; }
    set
    {
        state = value;
        RaiseNotifyPropertyChanged("State");
    }
}

在 XAML 中声明您的转换器:

<UserControl.Resources>
    <converters:StateStyleConverter x:Key="StateStyleConverter"/>
</UserControl.Resources>

最后将其绑定到按钮:

<Button Width="72" Height="72" Style="{Binding State, Converter={StaticResource StateStyleConverter}}" 
            DataContext="{Binding ElementName=this, Path=DataContext}"
            cmd:ButtonBaseExtensions.Command="{Binding PlayStopCommand}"
            />

You should define the playing state in you viewmodel (Playing/Stopped), and bind Button.Style to that property using a converter. In your converter, return a different style (taken from App.Current.Resources) based on the current state.

Edit:

Here's an example of your converter should look like:

public class StateStyleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (PlaybackState)value == PlaybackState.Playing ? App.Current.Resources["RoundPlay"] : App.Current.Resources["RoundStop"];
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

In this example, PlaybackState is an enum:

public enum PlaybackState
{
    Playing,
    Stopped
}

Then you should add the state property to your view model (The part where you notify the change depends on the framework you are using for MVVM):

private PlaybackState state;
public PlaybackState State
{
    get { return state; }
    set
    {
        state = value;
        RaiseNotifyPropertyChanged("State");
    }
}

Declare your converter in XAML:

<UserControl.Resources>
    <converters:StateStyleConverter x:Key="StateStyleConverter"/>
</UserControl.Resources>

And finally bind it to the button:

<Button Width="72" Height="72" Style="{Binding State, Converter={StaticResource StateStyleConverter}}" 
            DataContext="{Binding ElementName=this, Path=DataContext}"
            cmd:ButtonBaseExtensions.Command="{Binding PlayStopCommand}"
            />
魔法唧唧 2024-12-07 13:43:22

您可以使用 ToggleButton 并在选中/未选中的视觉状态中进行必要的视觉更改。

如果您必须按照问题所述的方式进行操作,那么您可以在资源中定义样式,然后在 this.Resources["YourStyleKey"];< 的代码隐藏中访问它/code> 您的问题是将其从视图获取到视图模型,因此我的第一个建议:)

You could use a ToggleButton and make the necessary visual changes in the visual states for checked/unchecked.

If you must do it the way your question states, then you can define the Style in the resources and then access it in the code-behind from this.Resources["YourStyleKey"]; Your problem will be getting it from the view to the view model, hence my first suggestion :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文