0

I'm trying to do a DropDownButton (or ComboButton) in Pure XAML because I want to use the GUI in PowerShell.

I'm aware of wpftoolkit's DropDownButton but it needs custom C#/c++ code to make it work.(and if I could bring it in, the wpftoolkit would be bigger than my project!)

Below I just stacked a Button control on top of a comboBox control. (Ugly, I know but my options are limited from my point of view)

                                    <Window
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                Height="580"
                MinHeight="580"
                MinWidth="700"
                Width="700"
                Background="lightgray">
                <Grid>
                    <ComboBox
                        x:Name="CboxWakeUp"
                        Width="100"
                        Height="80"
                        HorizontalAlignment="Right"
                        Margin="10,10,10,0"
                        VerticalAlignment="Top"
                        Background="green"
                        IsEditable="True"
                        IsReadOnly="True"
                        TextBlock.FontSize="16"
                        >
                        <ComboBoxItem Content="WakeUp and RDP" IsSelected="True"/>
                        <ComboBoxItem Content="WakeUp from list"/>
                        <ComboBoxItem Content="WakeUp and Run"/>
                    </ComboBox>
                    <!-- Button is ON TOP of ComboBox -->
                    <Button
                        Name="btnWakeUp"
                        Width="85"
                        Height="80"
                        HorizontalAlignment="Right"
                        Margin="10,10,25,0"
                        VerticalAlignment="Top"
                        Background="green"
                        TextBlock.FontSize="16"
                        >
                        <!-- This would makes the SelectedValue text wrap but the binding binding syntax  used is taken literally -->
                  <TextBlock Text="{Binding SelectedValue, ElementName=CboxWakeUp}" TextWrapping="Wrap" TextAlignment="Center"/>

                    </Button>
                </Grid>
            </Window>

I'm close but I need help. I need to wrap the text of the button and I have figured it out by embedding a TextBlock control inside the Button control (Thank you TheMadTechnician! ) but the binding syntax I used in the button is taken literally.

NOTE: I had the binding to the button working directly but when a selection was made in the ComboBox, Kaxaml gave me "Must disconnect specified child from current parent Visual before attaching to new parent Visual" error. I'm hoping the TextBlock control will side-step this issue.

PS: I was going to try to make the button change colour based on the selection made in the ComboBox:

  • WakeUp and RDP ==> Green
  • WakeUp from list ==> Blue
  • WakeUp and Run ==> Red

This will have to be done in PS code and that's ok

Mr. Annoyed
  • 541
  • 3
  • 18
  • So if this were me I wouldn't do it in the XAML. I'd create the objects in PowerShell, and add event triggers to the combo box that updates the button's text and color with the OnUpdate event or something. – TheMadTechnician Oct 27 '17 at 17:59
  • That's what I did in the previous WinForms version of the app but I need to simplify code and I've seen binding done for stuff like this in XAML. I'm hoping to let XAML do the GUI stuff so that I can focus on the guts on the powershell side. – Mr. Annoyed Oct 27 '17 at 18:23
  • 1
    The colors at least you can do in XAML by defining them in the button's style. Check out [this question's](https://stackoverflow.com/questions/11654409/how-to-change-custom-controls-background-color-based-on-its-property) answers for examples of changing color depending on the property of a second control. Check [this one](https://stackoverflow.com/questions/754137/wpf-button-textwrap-style) for button text wrapping. – TheMadTechnician Oct 27 '17 at 23:17
  • used the TextBlock idea to wrap text. Thank you! I have updated the XAML and almost have the Binding working. syntax issue. – Mr. Annoyed Oct 30 '17 at 17:27

0 Answers0