WPF CheckBox with "Button" appearance - wpf

I need a button-like control that can have a Checked property, so that when clicked it stays pressed.
I had that functionality in WinForms, with the CheckBox control, setting the Appearance property to "Button".
Can someone help me?

Use a ToggleButton, it has all the functionality you see in a CheckBox since it is derived from it.

WPF has a built-in ToggleButton control that serves this purpose. If you need to change the visual appearance of this default control you will need to apply a new Template (ControlTemplate) to it.

<Window.BindingGroup>
<BindingGroup Name="{x:Null}" NotifyOnValidationError="False" />
</Window.BindingGroup>
<Grid>
<nit:checkbutton1 x:Name="button1" Margin="32,88,0,0" Click="checkbutton1_Click" HorizontalAlignment="Left" Width="31" Height="32" VerticalAlignment="Top" mode="{Binding ElementName=cb1, Path=SelectedItem}" />
<ComboBox x:Name="cb1" ItemsSource="{Binding Source={StaticResource modeEnum}}" IsSynchronizedWithCurrentItem="True" Height="23" Margin="0,97,24,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="112" />
</Grid>

Related

Click through ListBox to underlying Control

Hi I want to click through my listBox (click on empty space) and want the click on the underlying border control. I'm really sure that I have done this in the past using {x:Null} as the Background for a control. But this time it doesn't work.
Any hint why?
<Grid>
<Border x:Name="BrushBorder" Background="{Binding ActualBrush}" Margin="7,0" Height="10"
VerticalAlignment="Top" Cursor="Pen">
<dxmvvm:Interaction.Behaviors>
<dxmvvm:EventToCommand EventName="MouseDown" Command="{Binding NewGradientStopCommand}" PassEventArgsToCommand="True" />
</dxmvvm:Interaction.Behaviors>
</Border>
<ListBox x:Name="GradientStopListBox" ItemsSource="{Binding GradientStops}" SelectedItem="{Binding ActualGradientStop}"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
Background="{x:Null}" BorderBrush="{x:Null}">
I found the answer. The ListBox includes a ScrollViewer and this is causing the mouse click the stay in the ListBox regradless of the BackgroundBrush.
See this question for further information:
Transparent WPF ListBox with selectable items

Create user control in wpf

Please can anyone tell me how to make a combobox which contains items as a label controls and each label control has delete button on its left side. If click on delete button this item will be deleted and added to the listbox.
I have tried this but I am not able to do this.
*The Below code is the exactly doing the same. *
<ComboBox Width="100" Height="23">
<ComboBoxItem>
<StackPanel Orientation="Horizontal">
<Button Content="Delete" Click="Button_Click" Name="btn"/>
<Label Foreground="Red">Red</Label>
</StackPanel>
</ComboBoxItem>
</ComboBox>

Disabling a controls FocusVisualStyle?

I'm trying to disable the FocusVisualStyle of a newly created textbox. As far as I understand you just need to add: FocusVisualStyle="{x:Null}" but for some reason the border of the textbox is still changing. The full thing is:
<TextBox FocusVisualStyle="{x:Null}" Height="82" HorizontalAlignment="Left" Margin="142,264,0,0" Name="textBox1" VerticalAlignment="Top" Width="169" BorderBrush="Black" />
Did I do something wrong?
Try setting BorderThickness="0" , better yet create a trigger when does that when IsFocused = True

Pattern for working with a form in Silverlight 4? (How to get references to XAML elements)

I have a form:
<StackPanel Orientation="Horizontal" Visibility="{Binding Editable, Converter={StaticResource visibilityConverter}}"
ToolTipService.ToolTip="Add new topic to this group">
<sdk:AutoCompleteBox Width="160" ItemsSource="{Binding ElementName=LayoutRoot, Path=DataContext.TopicNames}" />
<Button Click="addTopicButton_Click">
<Image Source="Images/appbar.add.rest.png" />
</Button>
</StackPanel>
This form appears in a DataTemplate for an ItemsControl. I'm not sure what the best way is to get the data from the AutoCompleteBox when the button is clicked. I can't give the elements x:Name attributes, because they're in a template (right?).
How can I get around this? The Click event will give me the Button, but I need a reference to the text box. Use the Button's parent, then look through the children for the Textbox? If I factored this out into its own UserControl, I could set x:Name values, but I'd rather not do that.
Any other ideas?
Update: Here is another example of such a problem:
<ListBox x:Name="topicList"
ItemsSource="{Binding Id, Converter={StaticResource topicGroupIDConverter}}"
SelectionChanged="ListBox_SelectionChanged"
HorizontalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"
Width="150"
VerticalAlignment="Center"
ToolTipService.ToolTip="{Binding Description}"
ToolTipService.Placement="Right" />
<Button ToolTipService.ToolTip="Remove this topic from this group"
Visibility="{Binding ElementName=topicList,
Path=DataContext.Editable,
Converter={StaticResource visibilityConverter}}"
Click="removeTopicButton_Click"
HorizontalAlignment="Right"
Margin="10,0">
<Image Source="Images/appbar.cancel.rest.png" />
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When the button is clicked, I want to access topicList.DataContext. However, topicList itself is a DataTemplate in an ItemsControl, so I can't access it using its name from code-behind. How else can I do this?
You can add a property, say SelectedItemInAutoCompleteBox, to your presenter, and then can bind it to the SelectedItem property of AutoCompleteBox, using Mode=TwoWay, like this,
<sdk:AutoCompleteBox SelectedItem="{Binding Path=DataContext.SelectedItemInAutoCompleteBox, Mode=TwoWay}" ... />
You may try the same approach with Text property of AutoCompleteBox, also. See if it solves your problem.:-)
You have several choices:
If you're on Silverlight 5, use the AncestorBinding
Otherwise, use a Silverlight 4 AncestorBinding hack (it doesn't look pretty)
Or you could try DataContextProxy, which stores the DataContext in a resource so that it is accessible. Note: you should set the DataContextProxy as a Resource of topicList ListBox, not the UserControl as in Dan Wahlin's example.

How to dynamically add controls to a Silverlight Button?

I'm working on a Silverlight Polling control. My goal is to have a Button that contains other controls, so that when a user clicks on this combination, stuff happens.
Here's the basic XAML:
<Button Grid.Row="1" Grid.Column="0" Style="{StaticResource AnswerButton}">
<StackPanel Grid.Row="1" Grid.Column="0" Height="Auto" Width="Auto" Style="{StaticResource StackPnl}">
<TextBox Text="this is a text box" Style="{StaticResource Answer}" />
<ProgressBar Style="{StaticResource PollProgress}" />
</StackPanel>
</Button>
That works fine, but assume I don't have the StackPanel, etc, defined:
<Button Grid.Row="1" Grid.Column="0" Style="{StaticResource AnswerButton}">
</Button>
But that I want to add the StackPanel, TextBox, and ProgressBar at runtime.
Looking at just the first hurdle that I've run into, how do I dynamically add the StackPanel to the Button control?
Alternatively, if someone knows of a better way to do this...
var panel = new StackPanel();
Button.Content = panel;

Resources