Vertical WPF icon button that used Segoe MDL2 font for icon












0














This is a button that changes text and icon according to a Boolean state. for example, a play / pause button or a connect / disconnect button. The icon uses Segoe MDL2 Assets as the default font, but you can edit the template to change to another!



The code:



public class StateImageButton : Button
{
static StateImageButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(StateImageButton), new FrameworkPropertyMetadata(typeof(StateImageButton)));
}

public bool State
{
get { return (bool)GetValue(StateProperty); }
set { SetValue(StateProperty, value); }
}

public Orientation Orientation
{
get { return (Orientation)GetValue(OrientationProperty); }
set { SetValue(OrientationProperty, value); }
}

public String TextOn
{
get { return (String)GetValue(TextOnProperty); }
set { SetValue(TextOnProperty, value); }
}

public String IconOn
{
get { return (String)GetValue(IconOnProperty); }
set { SetValue(IconOnProperty, value); }
}

public String TextOff
{
get { return (String)GetValue(TextOffProperty); }
set { SetValue(TextOffProperty, value); }
}

public String IconOff
{
get { return (String)GetValue(IconOffProperty); }
set { SetValue(IconOffProperty, value); }
}

public static readonly DependencyProperty StateProperty =
DependencyProperty.Register("State",
typeof(bool),
typeof(StateImageButton),
new PropertyMetadata(false));

public static readonly DependencyProperty OrientationProperty =
DependencyProperty.Register("Orientation",
typeof(Orientation),
typeof(StateImageButton),
new PropertyMetadata(Orientation.Vertical));

public static readonly DependencyProperty TextOnProperty =
DependencyProperty.Register("TextOn",
typeof(string),
typeof(StateImageButton),
new PropertyMetadata(""));

public static readonly DependencyProperty IconOnProperty =
DependencyProperty.Register("IconOn",
typeof(string),
typeof(StateImageButton),
new PropertyMetadata(""));

public static readonly DependencyProperty TextOffProperty =
DependencyProperty.Register("TextOff",
typeof(string),
typeof(StateImageButton),
new PropertyMetadata(""));

public static readonly DependencyProperty IconOffProperty =
DependencyProperty.Register("IconOff",
typeof(string),
typeof(StateImageButton),
new PropertyMetadata(""));
}


The style (note that it is based on the ToolBar button style, as I prefer this, but you can change it to a normal button style too)



<Style TargetType="{x:Type local:StateImageButton}" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
<Setter Property="MinWidth" Value="{Binding Path=ActualHeight, RelativeSource={RelativeSource Self}}"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Margin="5"
Orientation="{Binding Orientation, RelativeSource={RelativeSource AncestorType=Button}}">
<TextBlock Name="Icon"
FontFamily="Segoe MDL2 Assets"
HorizontalAlignment="Center"
FontSize="18"
Text="{Binding Icon, RelativeSource={RelativeSource AncestorType=Button}}" />
<TextBlock Name="Text"
Text="{Binding Text, RelativeSource={RelativeSource AncestorType=Button}}"
HorizontalAlignment="Center"
Margin="0,5,0,0" />
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding State, RelativeSource={RelativeSource AncestorType=Button}}" Value="True">
<Setter TargetName="Text" Property="Text" Value="{Binding TextOn, RelativeSource={RelativeSource AncestorType=Button}}"/>
</DataTrigger>
<DataTrigger Binding="{Binding State, RelativeSource={RelativeSource AncestorType=Button}}" Value="False">
<Setter TargetName="Text" Property="Text" Value="{Binding TextOff, RelativeSource={RelativeSource AncestorType=Button}}"/>
</DataTrigger>
<DataTrigger Binding="{Binding State, RelativeSource={RelativeSource AncestorType=Button}}" Value="True">
<Setter TargetName="Icon" Property="Text" Value="{Binding IconOn, RelativeSource={RelativeSource AncestorType=Button}}"/>
</DataTrigger>
<DataTrigger Binding="{Binding State, RelativeSource={RelativeSource AncestorType=Button}}" Value="False">
<Setter TargetName="Icon" Property="Text" Value="{Binding IconOff, RelativeSource={RelativeSource AncestorType=Button}}"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>


and an example of use



<local:StateImageButton State="{Binding TheBooleanState}"
TextOff="Start"
TextOn="Stop"
IconOff=""
IconOn=""
Command="{Binding TheCommand}"/>








share



























    0














    This is a button that changes text and icon according to a Boolean state. for example, a play / pause button or a connect / disconnect button. The icon uses Segoe MDL2 Assets as the default font, but you can edit the template to change to another!



    The code:



    public class StateImageButton : Button
    {
    static StateImageButton()
    {
    DefaultStyleKeyProperty.OverrideMetadata(typeof(StateImageButton), new FrameworkPropertyMetadata(typeof(StateImageButton)));
    }

    public bool State
    {
    get { return (bool)GetValue(StateProperty); }
    set { SetValue(StateProperty, value); }
    }

    public Orientation Orientation
    {
    get { return (Orientation)GetValue(OrientationProperty); }
    set { SetValue(OrientationProperty, value); }
    }

    public String TextOn
    {
    get { return (String)GetValue(TextOnProperty); }
    set { SetValue(TextOnProperty, value); }
    }

    public String IconOn
    {
    get { return (String)GetValue(IconOnProperty); }
    set { SetValue(IconOnProperty, value); }
    }

    public String TextOff
    {
    get { return (String)GetValue(TextOffProperty); }
    set { SetValue(TextOffProperty, value); }
    }

    public String IconOff
    {
    get { return (String)GetValue(IconOffProperty); }
    set { SetValue(IconOffProperty, value); }
    }

    public static readonly DependencyProperty StateProperty =
    DependencyProperty.Register("State",
    typeof(bool),
    typeof(StateImageButton),
    new PropertyMetadata(false));

    public static readonly DependencyProperty OrientationProperty =
    DependencyProperty.Register("Orientation",
    typeof(Orientation),
    typeof(StateImageButton),
    new PropertyMetadata(Orientation.Vertical));

    public static readonly DependencyProperty TextOnProperty =
    DependencyProperty.Register("TextOn",
    typeof(string),
    typeof(StateImageButton),
    new PropertyMetadata(""));

    public static readonly DependencyProperty IconOnProperty =
    DependencyProperty.Register("IconOn",
    typeof(string),
    typeof(StateImageButton),
    new PropertyMetadata(""));

    public static readonly DependencyProperty TextOffProperty =
    DependencyProperty.Register("TextOff",
    typeof(string),
    typeof(StateImageButton),
    new PropertyMetadata(""));

    public static readonly DependencyProperty IconOffProperty =
    DependencyProperty.Register("IconOff",
    typeof(string),
    typeof(StateImageButton),
    new PropertyMetadata(""));
    }


    The style (note that it is based on the ToolBar button style, as I prefer this, but you can change it to a normal button style too)



    <Style TargetType="{x:Type local:StateImageButton}" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
    <Setter Property="MinWidth" Value="{Binding Path=ActualHeight, RelativeSource={RelativeSource Self}}"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="ContentTemplate">
    <Setter.Value>
    <DataTemplate>
    <StackPanel Margin="5"
    Orientation="{Binding Orientation, RelativeSource={RelativeSource AncestorType=Button}}">
    <TextBlock Name="Icon"
    FontFamily="Segoe MDL2 Assets"
    HorizontalAlignment="Center"
    FontSize="18"
    Text="{Binding Icon, RelativeSource={RelativeSource AncestorType=Button}}" />
    <TextBlock Name="Text"
    Text="{Binding Text, RelativeSource={RelativeSource AncestorType=Button}}"
    HorizontalAlignment="Center"
    Margin="0,5,0,0" />
    </StackPanel>
    <DataTemplate.Triggers>
    <DataTrigger Binding="{Binding State, RelativeSource={RelativeSource AncestorType=Button}}" Value="True">
    <Setter TargetName="Text" Property="Text" Value="{Binding TextOn, RelativeSource={RelativeSource AncestorType=Button}}"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding State, RelativeSource={RelativeSource AncestorType=Button}}" Value="False">
    <Setter TargetName="Text" Property="Text" Value="{Binding TextOff, RelativeSource={RelativeSource AncestorType=Button}}"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding State, RelativeSource={RelativeSource AncestorType=Button}}" Value="True">
    <Setter TargetName="Icon" Property="Text" Value="{Binding IconOn, RelativeSource={RelativeSource AncestorType=Button}}"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding State, RelativeSource={RelativeSource AncestorType=Button}}" Value="False">
    <Setter TargetName="Icon" Property="Text" Value="{Binding IconOff, RelativeSource={RelativeSource AncestorType=Button}}"/>
    </DataTrigger>
    </DataTemplate.Triggers>
    </DataTemplate>
    </Setter.Value>
    </Setter>
    </Style>


    and an example of use



    <local:StateImageButton State="{Binding TheBooleanState}"
    TextOff="Start"
    TextOn="Stop"
    IconOff=""
    IconOn=""
    Command="{Binding TheCommand}"/>








    share

























      0












      0








      0







      This is a button that changes text and icon according to a Boolean state. for example, a play / pause button or a connect / disconnect button. The icon uses Segoe MDL2 Assets as the default font, but you can edit the template to change to another!



      The code:



      public class StateImageButton : Button
      {
      static StateImageButton()
      {
      DefaultStyleKeyProperty.OverrideMetadata(typeof(StateImageButton), new FrameworkPropertyMetadata(typeof(StateImageButton)));
      }

      public bool State
      {
      get { return (bool)GetValue(StateProperty); }
      set { SetValue(StateProperty, value); }
      }

      public Orientation Orientation
      {
      get { return (Orientation)GetValue(OrientationProperty); }
      set { SetValue(OrientationProperty, value); }
      }

      public String TextOn
      {
      get { return (String)GetValue(TextOnProperty); }
      set { SetValue(TextOnProperty, value); }
      }

      public String IconOn
      {
      get { return (String)GetValue(IconOnProperty); }
      set { SetValue(IconOnProperty, value); }
      }

      public String TextOff
      {
      get { return (String)GetValue(TextOffProperty); }
      set { SetValue(TextOffProperty, value); }
      }

      public String IconOff
      {
      get { return (String)GetValue(IconOffProperty); }
      set { SetValue(IconOffProperty, value); }
      }

      public static readonly DependencyProperty StateProperty =
      DependencyProperty.Register("State",
      typeof(bool),
      typeof(StateImageButton),
      new PropertyMetadata(false));

      public static readonly DependencyProperty OrientationProperty =
      DependencyProperty.Register("Orientation",
      typeof(Orientation),
      typeof(StateImageButton),
      new PropertyMetadata(Orientation.Vertical));

      public static readonly DependencyProperty TextOnProperty =
      DependencyProperty.Register("TextOn",
      typeof(string),
      typeof(StateImageButton),
      new PropertyMetadata(""));

      public static readonly DependencyProperty IconOnProperty =
      DependencyProperty.Register("IconOn",
      typeof(string),
      typeof(StateImageButton),
      new PropertyMetadata(""));

      public static readonly DependencyProperty TextOffProperty =
      DependencyProperty.Register("TextOff",
      typeof(string),
      typeof(StateImageButton),
      new PropertyMetadata(""));

      public static readonly DependencyProperty IconOffProperty =
      DependencyProperty.Register("IconOff",
      typeof(string),
      typeof(StateImageButton),
      new PropertyMetadata(""));
      }


      The style (note that it is based on the ToolBar button style, as I prefer this, but you can change it to a normal button style too)



      <Style TargetType="{x:Type local:StateImageButton}" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
      <Setter Property="MinWidth" Value="{Binding Path=ActualHeight, RelativeSource={RelativeSource Self}}"/>
      <Setter Property="Padding" Value="0"/>
      <Setter Property="ContentTemplate">
      <Setter.Value>
      <DataTemplate>
      <StackPanel Margin="5"
      Orientation="{Binding Orientation, RelativeSource={RelativeSource AncestorType=Button}}">
      <TextBlock Name="Icon"
      FontFamily="Segoe MDL2 Assets"
      HorizontalAlignment="Center"
      FontSize="18"
      Text="{Binding Icon, RelativeSource={RelativeSource AncestorType=Button}}" />
      <TextBlock Name="Text"
      Text="{Binding Text, RelativeSource={RelativeSource AncestorType=Button}}"
      HorizontalAlignment="Center"
      Margin="0,5,0,0" />
      </StackPanel>
      <DataTemplate.Triggers>
      <DataTrigger Binding="{Binding State, RelativeSource={RelativeSource AncestorType=Button}}" Value="True">
      <Setter TargetName="Text" Property="Text" Value="{Binding TextOn, RelativeSource={RelativeSource AncestorType=Button}}"/>
      </DataTrigger>
      <DataTrigger Binding="{Binding State, RelativeSource={RelativeSource AncestorType=Button}}" Value="False">
      <Setter TargetName="Text" Property="Text" Value="{Binding TextOff, RelativeSource={RelativeSource AncestorType=Button}}"/>
      </DataTrigger>
      <DataTrigger Binding="{Binding State, RelativeSource={RelativeSource AncestorType=Button}}" Value="True">
      <Setter TargetName="Icon" Property="Text" Value="{Binding IconOn, RelativeSource={RelativeSource AncestorType=Button}}"/>
      </DataTrigger>
      <DataTrigger Binding="{Binding State, RelativeSource={RelativeSource AncestorType=Button}}" Value="False">
      <Setter TargetName="Icon" Property="Text" Value="{Binding IconOff, RelativeSource={RelativeSource AncestorType=Button}}"/>
      </DataTrigger>
      </DataTemplate.Triggers>
      </DataTemplate>
      </Setter.Value>
      </Setter>
      </Style>


      and an example of use



      <local:StateImageButton State="{Binding TheBooleanState}"
      TextOff="Start"
      TextOn="Stop"
      IconOff=""
      IconOn=""
      Command="{Binding TheCommand}"/>








      share













      This is a button that changes text and icon according to a Boolean state. for example, a play / pause button or a connect / disconnect button. The icon uses Segoe MDL2 Assets as the default font, but you can edit the template to change to another!



      The code:



      public class StateImageButton : Button
      {
      static StateImageButton()
      {
      DefaultStyleKeyProperty.OverrideMetadata(typeof(StateImageButton), new FrameworkPropertyMetadata(typeof(StateImageButton)));
      }

      public bool State
      {
      get { return (bool)GetValue(StateProperty); }
      set { SetValue(StateProperty, value); }
      }

      public Orientation Orientation
      {
      get { return (Orientation)GetValue(OrientationProperty); }
      set { SetValue(OrientationProperty, value); }
      }

      public String TextOn
      {
      get { return (String)GetValue(TextOnProperty); }
      set { SetValue(TextOnProperty, value); }
      }

      public String IconOn
      {
      get { return (String)GetValue(IconOnProperty); }
      set { SetValue(IconOnProperty, value); }
      }

      public String TextOff
      {
      get { return (String)GetValue(TextOffProperty); }
      set { SetValue(TextOffProperty, value); }
      }

      public String IconOff
      {
      get { return (String)GetValue(IconOffProperty); }
      set { SetValue(IconOffProperty, value); }
      }

      public static readonly DependencyProperty StateProperty =
      DependencyProperty.Register("State",
      typeof(bool),
      typeof(StateImageButton),
      new PropertyMetadata(false));

      public static readonly DependencyProperty OrientationProperty =
      DependencyProperty.Register("Orientation",
      typeof(Orientation),
      typeof(StateImageButton),
      new PropertyMetadata(Orientation.Vertical));

      public static readonly DependencyProperty TextOnProperty =
      DependencyProperty.Register("TextOn",
      typeof(string),
      typeof(StateImageButton),
      new PropertyMetadata(""));

      public static readonly DependencyProperty IconOnProperty =
      DependencyProperty.Register("IconOn",
      typeof(string),
      typeof(StateImageButton),
      new PropertyMetadata(""));

      public static readonly DependencyProperty TextOffProperty =
      DependencyProperty.Register("TextOff",
      typeof(string),
      typeof(StateImageButton),
      new PropertyMetadata(""));

      public static readonly DependencyProperty IconOffProperty =
      DependencyProperty.Register("IconOff",
      typeof(string),
      typeof(StateImageButton),
      new PropertyMetadata(""));
      }


      The style (note that it is based on the ToolBar button style, as I prefer this, but you can change it to a normal button style too)



      <Style TargetType="{x:Type local:StateImageButton}" BasedOn="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
      <Setter Property="MinWidth" Value="{Binding Path=ActualHeight, RelativeSource={RelativeSource Self}}"/>
      <Setter Property="Padding" Value="0"/>
      <Setter Property="ContentTemplate">
      <Setter.Value>
      <DataTemplate>
      <StackPanel Margin="5"
      Orientation="{Binding Orientation, RelativeSource={RelativeSource AncestorType=Button}}">
      <TextBlock Name="Icon"
      FontFamily="Segoe MDL2 Assets"
      HorizontalAlignment="Center"
      FontSize="18"
      Text="{Binding Icon, RelativeSource={RelativeSource AncestorType=Button}}" />
      <TextBlock Name="Text"
      Text="{Binding Text, RelativeSource={RelativeSource AncestorType=Button}}"
      HorizontalAlignment="Center"
      Margin="0,5,0,0" />
      </StackPanel>
      <DataTemplate.Triggers>
      <DataTrigger Binding="{Binding State, RelativeSource={RelativeSource AncestorType=Button}}" Value="True">
      <Setter TargetName="Text" Property="Text" Value="{Binding TextOn, RelativeSource={RelativeSource AncestorType=Button}}"/>
      </DataTrigger>
      <DataTrigger Binding="{Binding State, RelativeSource={RelativeSource AncestorType=Button}}" Value="False">
      <Setter TargetName="Text" Property="Text" Value="{Binding TextOff, RelativeSource={RelativeSource AncestorType=Button}}"/>
      </DataTrigger>
      <DataTrigger Binding="{Binding State, RelativeSource={RelativeSource AncestorType=Button}}" Value="True">
      <Setter TargetName="Icon" Property="Text" Value="{Binding IconOn, RelativeSource={RelativeSource AncestorType=Button}}"/>
      </DataTrigger>
      <DataTrigger Binding="{Binding State, RelativeSource={RelativeSource AncestorType=Button}}" Value="False">
      <Setter TargetName="Icon" Property="Text" Value="{Binding IconOff, RelativeSource={RelativeSource AncestorType=Button}}"/>
      </DataTrigger>
      </DataTemplate.Triggers>
      </DataTemplate>
      </Setter.Value>
      </Setter>
      </Style>


      and an example of use



      <local:StateImageButton State="{Binding TheBooleanState}"
      TextOff="Start"
      TextOn="Stop"
      IconOff=""
      IconOn=""
      Command="{Binding TheCommand}"/>






      wpf xaml





      share












      share










      share



      share










      asked 1 min ago









      geometrikal

      190213




      190213



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210639%2fvertical-wpf-icon-button-that-used-segoe-mdl2-font-for-icon%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210639%2fvertical-wpf-icon-button-that-used-segoe-mdl2-font-for-icon%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Morgemoulin

          Scott Moir

          Souastre