Search code examples
wpfvisual-studioxamldependency-propertieswpf-style

ControlTemplate Trigger don't work in designer (runtime ok)


I have a custom Control derived from Window:

class LVSDialog : Window

with DependencyProperty ShowCloseButton

and a Style with ControlTemplate and Trigger:

<Style TargetType="{x:Type loc:LVSDialog}" x:Key="LVSDialogStyle">
    ...
    <Setter Property="Template">
        ...
        <Button x:Name="closeButton" />
        ...
        <ControlTemplate.Triggers>
            <Trigger Property="loc:LVSDialog.ShowCloseButton" Value="False">
                <Setter TargetName="closeButton" Property="Visibility" Value="Collapsed" />
            </Trigger>
        </ControlTemplate.Triggers>            
    </Setter>

Everything works fine in the runtime, but in Designer it doesn't take sence if I change this Property - Button is visible all the time:

<loc:LVSDialog ...
    ShowCloseButton="False" Style="{StaticResource LVSDialogStyle}">

I have searched for a solution in google and here - all questions are about runtime functionality, designer problems are either unanswered or not working suggestions.

Is it possible at all use full functions in design time?

P.S. My VisualStudio is 2012. Framework 4.0


Solution

  • If you change the base class to Control instead of Window it will work:

      public class LVSDialog : Control
      {
    
    
        public bool ShowCloseButton
        {
          get { return (bool)GetValue(ShowCloseButtonProperty); }
          set { SetValue(ShowCloseButtonProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for ShowCloseButton.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ShowCloseButtonProperty =
            DependencyProperty.Register("ShowCloseButton", typeof(bool), typeof(LVSDialog), new PropertyMetadata(true));
    
    
      }
    

    From Topicstarter:

    I have changed to Control, added internal window, set it content to my control and added Show() and ShowDialog() methods:

    private Window parentWindow;
    ...
    public void Show()
    {
        if (parentWindow == null)
        {
            parentWindow = new Window {Content = this, WindowStyle = ...};
        }
        parentWindow.Show();
    }
    

    Everything works fine, designer shows all properties "live".