Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 一个播放按钮控制WPF的所有其他按钮操作_C#_Wpf_Animation_Button - Fatal编程技术网

C# 一个播放按钮控制WPF的所有其他按钮操作

C# 一个播放按钮控制WPF的所有其他按钮操作,c#,wpf,animation,button,C#,Wpf,Animation,Button,可能非常简单,但我很难找到我的代码中需要更改哪些部分才能使其正常工作。我的媒体位如下所示: 当我点击任意一个按钮时,其他三个按钮也会改变 public void ButtonPlay1_OnClick(object sender, RoutedEventArgs e) { var context = DataContext as PlayButton.SampleContext; if (context == null)

可能非常简单,但我很难找到我的代码中需要更改哪些部分才能使其正常工作。我的媒体位如下所示:

当我点击任意一个按钮时,其他三个按钮也会改变

        public void ButtonPlay1_OnClick(object sender, RoutedEventArgs e)
    {
        var context = DataContext as PlayButton.SampleContext;

        if (context == null)
            return;

        context.IsPlaying = !context.IsPlaying;
    }

    public void ButtonPlay2_OnClick(object sender, RoutedEventArgs e)
    {
        var context = DataContext as PlayButton.SampleContext;

        if (context == null)
            return;

        context.IsPlaying = !context.IsPlaying;
    }

    public void ButtonPlay3_OnClick(object sender, RoutedEventArgs e)
    {
        var context = DataContext as PlayButton.SampleContext;

        if (context == null)
            return;

        context.IsPlaying = !context.IsPlaying;
    }
这里有三个不同的按钮点击事件。我有一个类是booltovisibilityconverter,还有一个类叫做PlayButton,它只是检查按钮是否正在播放,这就是PlayButton类中的内容:

    public partial class PlayButton : xamlNewAudit
{
    public PlayButton()
    {
        InitializeComponent();
        DataContext = new SampleContext();
    }

    public class SampleContext : INotifyPropertyChanged
    {
        private bool _isPlaying;

        public bool IsPlaying
        {
            get { return _isPlaying; }
            set
            {
                if (_isPlaying == value)
                    return;

                _isPlaying = value;

                OnPropertyChanged("IsPlaying");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;

            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
编辑:对不起,简单解释一下,我只是想让它在我点击其中一个按钮时,其他两个按钮保持暂停

编辑:按钮的Xaml代码为

            <Button x:Name="ButtonPlay" Height="45" Click="ButtonPlay1_OnClick" Margin="-2,0,57,60" DockPanel.Dock="Top">
            <Button.Template>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Ellipse Stroke="Black" />

                        <Image Source="Images/PlayButton.png" Visibility="{Binding IsPlaying, ConverterParameter={x:Static Visibility.Hidden}, Converter={Application:BoolToVisibilityConverter}}" />

                        <Image Source="Images/pauseButton.png" Visibility="{Binding IsPlaying, ConverterParameter={x:Static Visibility.Visible}, Converter={Application:BoolToVisibilityConverter}}" />
                    </Grid>
                </ControlTemplate>
            </Button.Template>
        </Button>


这是第一个按钮的XAML代码,除了指向不同方法的单击事件链接外,其他按钮的代码相同。

如果您对所有三个按钮的IsPlaying绑定相同,则当IsPlaying更改时,所有三个按钮都将更改。最简单的改变方法是为每个按钮分别设置一个isplay属性。

您有三个
按钮
控件。您需要三个不同的
IsPlaying
属性,每个
按钮都有一个属性。您需要三个不同的
单击
处理程序,它们使用三个不同的
显示
属性。按照您的方式操作,您还需要三种不同的
ControlTemplate
s,它们也使用三种不同的
IsPlaying
属性


更新>>>

有很多更简单的方法来满足您的需求。一种方法是使用
切换按钮
而不是
按钮
。使用此选项,您可以忘记您的
IsPlaying
属性,而只需使用
ToggleButton。IsChecked
值:

<ControlTemplate TargetType="{x:Type ToggleButton}">
    <Grid>
        <Ellipse Stroke="Black" />
        <Image Source="Images/PlayButton.png" Visibility="{Binding IsChecked, 
ConverterParameter={x:Static Visibility.Hidden}, Converter={StaticResource 
Application:BoolToVisibilityConverter}}" />
        <Image Source="Images/pauseButton.png" Visibility="{Binding IsChecked, 
ConverterParameter={x:Static Visibility.Visible}, Converter={StaticResource 
Application:BoolToVisibilityConverter}}" />
    </Grid>
</ControlTemplate>


您似乎非常奇怪地声明了您的
转换器
。。。编译完成了吗?

我想你可能想澄清你的问题。我可以得到你按钮的xaml代码吗?@Inisher我在底部添加了编辑,很抱歉做了简要解释。@NeelBhasin,很抱歉,在编辑中添加了代码。所以你想一次播放任何剪辑或其他东西吗?我在PlayButton类中又添加了两个属性,所以现在它们都指向不同的属性,没有任何变化。中断setter和getter,您将能够看到发生了什么-如果您的XAML正确地查看了不同的属性,那么可能有什么东西正在设置这三个属性。有没有更简单的方法来实现我想要的?