コード例 #1
0
        static void OnBehaviorChanged(DependencyObject obj, IMediaControlBehavior oldValue, IMediaControlBehavior newValue)
        {
            if (oldValue != null)
            {
                var contentControl = obj as ContentControl;
                if (contentControl != null)
                {
                    var buttonBase = obj as ButtonBase;
                    if (buttonBase != null)
                    {
                        buttonBase.Command = null;
                    }

#if !SILVERLIGHT && !WINDOWS80
                    var appbarButton = buttonBase as AppBarButton;
                    if (appbarButton != null)
                    {
                        appbarButton.Label = "";
                        appbarButton.Icon  = null;
                    }
                    else
                    {
                        var appbarToggleButton = buttonBase as AppBarToggleButton;
                        if (appbarToggleButton != null)
                        {
                            appbarToggleButton.Label = "";
                            appbarToggleButton.Icon  = null;
                        }
                        else
                        {
                            contentControl.Content = null;
                        }
                    }
#else
                    contentControl.Content = null;
#endif
                }
                else
                {
                    var textBlock = obj as TextBlock;
                    if (textBlock != null)
                    {
                        textBlock.Text = null;
                    }
                }
                AutomationProperties.SetName(obj, "");
                ToolTipService.SetToolTip(obj, null);
            }

            if (oldValue is IElementAwareMediaBehavior)
            {
                ((IElementAwareMediaBehavior)oldValue).Element = null;
            }
            if (newValue is IElementAwareMediaBehavior)
            {
                ((IElementAwareMediaBehavior)newValue).Element = obj;
            }

            if (newValue != null)
            {
                var contentControl = obj as ContentControl;
                if (contentControl != null)
                {
                    var buttonBase = obj as ButtonBase;
                    if (buttonBase != null)
                    {
                        buttonBase.Command = newValue.Command ?? buttonBase.Command;
                    }
#if !SILVERLIGHT && !WINDOWS80
                    var appbarButton = buttonBase as AppBarButton;
                    if (appbarButton != null)
                    {
                        appbarButton.SetBinding(AppBarButton.LabelProperty, new Binding()
                        {
                            Path = new PropertyPath("Label"), Source = newValue, Converter = newValue.LabelConverter
                        });
                        appbarButton.SetBinding(AppBarButton.IconProperty, new Binding()
                        {
                            Path = new PropertyPath("Content"), Source = newValue, Converter = newValue.ContentConverter
                        });
                    }
                    else
                    {
                        var appbarToggleButton = buttonBase as AppBarToggleButton;
                        if (appbarToggleButton != null)
                        {
                            appbarToggleButton.SetBinding(AppBarToggleButton.LabelProperty, new Binding()
                            {
                                Path = new PropertyPath("Label"), Source = newValue, Converter = newValue.LabelConverter
                            });
                            appbarToggleButton.SetBinding(AppBarToggleButton.IconProperty, new Binding()
                            {
                                Path = new PropertyPath("Content"), Source = newValue, Converter = newValue.ContentConverter
                            });
                        }
                        else
                        {
                            contentControl.SetBinding(ContentControl.ContentProperty, new Binding()
                            {
                                Path = new PropertyPath("Content"), Source = newValue, Converter = newValue.ContentConverter
                            });
                        }
                    }
#else
                    contentControl.SetBinding(ContentControl.ContentProperty, new Binding()
                    {
                        Path = new PropertyPath("Content"), Source = newValue, Converter = newValue.ContentConverter
                    });
#endif
                }
                else
                {
                    var textBlock = obj as TextBlock;
                    if (textBlock != null)
                    {
                        textBlock.SetBinding(TextBlock.TextProperty, new Binding()
                        {
                            Path = new PropertyPath("Content"), Source = newValue, Converter = newValue.ContentConverter
                        });
                    }
                }

                BindingOperations.SetBinding(obj, AutomationProperties.NameProperty, new Binding()
                {
                    Path = new PropertyPath("Label"), Source = newValue, Converter = newValue.LabelConverter
                });
                if (GetIsToolTipEnabled(obj))
                {
                    BindingOperations.SetBinding(obj, ToolTipService.ToolTipProperty, new Binding()
                    {
                        Path = new PropertyPath("Label"), Source = newValue, Converter = newValue.LabelConverter
                    });
                }
            }
        }
コード例 #2
0
 /// <summary>
 /// Sets the behavior on an object.
 /// </summary>
 /// <param name="obj">The object to set the behavior on.</param>
 /// <param name="value">The behavior to be associated with the object.</param>
 public static void SetBehavior(DependencyObject obj, IMediaControlBehavior value)
 {
     obj.SetValue(BehaviorProperty, value);
 }