Create user control in wpf - 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>

Related

DevExpress - Keep multi-selection on mouse click

I have a table, where in the first column I want to show a checkbox (currently of type CheckEdit) which enables the user to select/deselect items, and their name. Currently I have the following:
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<DockPanel VerticalAlignment="Center" LastChildFill="True">
<dxe:CheckEdit IsChecked="{Binding Path=RowData.DataContext.IsSelected}"
DockPanel.Dock="Left"
Margin="0,0,5,0"/>
<Border BorderBrush="{x:Null}"
VerticalAlignment="Center">
<TextBox Text="{Binding Path=RowData.DataContext.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
BorderThickness="0" />
</Border>
</DockPanel>
</DataTemplate>
</dxg:GridColumn.CellTemplate>
I have enabled multi-selection on the table, and I want it simply that if the user clicks one of the checkboxes, all of the selected checkboxes get the new value, and all of the rows stay selected.
What happenes now is that the CheckEdit-element that was clicked, gets a new value just as it should, all others remain unchanged, and the selection changes to only that specific row where the user clicked.
I have played around with GotFocus, PreviewMouseDown and MouseDown. None really worked as I want.
How can I get this behaviour?

Delete datagrid column programmatically

I have datagrid in wpf. When I do mouse-over on column header, There is one close button will appear.
I want to have some functionality so when I click on that button, whole column should be deleted.
I have wrote this.
<DataTemplate x:Key="AdornerDataTemplate">
<Grid HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,0,0">
<Button Content="X" Width="26" Height="26" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ctrls:RhinoDataGrid}}, Path=RemoveColumnCommand}" Background="{DynamicResource GridHeaderMouseOverBrush}"></Button>
</Grid>
I can see that RemoveColumnCommand is executed but it doesn't have index of column. How would I know on which column I clicked.
Pls help me.
Thanks
Dee
You can try to pass the column as CommandParameter via a binding. The index itself should not be needed.

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.

ComboBoxItem to full width of ComboBox, when using SelectedIndex, or navigating keyboard?

Following XAML causes the "My stuff" being centered of ComboBox until I open the ComboBox, when it correctly stretches itself.
<ComboBox Height="30" Width="300" HorizontalContentAlignment="Stretch" SelectedIndex="0">
<ComboBoxItem HorizontalContentAlignment="Stretch">
<Border Background="Red">
<TextBlock>My stuff...</TextBlock>
</Border>
</ComboBoxItem>
</ComboBox>
The question is, is it possible to get the ComboBoxItem being stretched even when it's selected using SelectedIndex? Same bug, or feature, happens if SelectedIndex is untouched (-1) and one selects the item using keyboard.
Workaround is probably to open the ComboBox programmatically in the beginning of app, which is rather ugly.
You just need to set the width of your border to the dynamic width of your outercontrol:
E.g.
Width="{Binding ElementName=combox1, Path=ActualWidth}">
Try this:
<ComboBox x:Name="combox1" Height="30" Width="300" HorizontalContentAlignment="Stretch"
SelectedIndex="0">
<ComboBoxItem HorizontalContentAlignment="Stretch">
<Border Background="Red" Width="{Binding ElementName=combox1, Path=ActualWidth}">
<TextBlock>My stuff...</TextBlock>
</Border>
</ComboBoxItem>
</ComboBox>
I see yes - i'm sure there is a way round this. It really depends what the end result is that you want. Do each of your data items have a difference background colour to identify them or is it simply a background colour to your whole combobox which you are trying to achieve.
If it is the latter, try this - and perhaps also remove the highlighted selection colour too, else perhaps the code behind route is correct, in terms of preselecting your first item, as that may be an option.
Example of All over Background colour:
<ComboBox Background="Red" x:Name="combox2" Height="30" HorizontalContentAlignment="Stretch" SelectedIndex="0">
<ComboBoxItem Background="Red" HorizontalContentAlignment="Stretch">
<TextBlock Background="Red">My stuff...</TextBlock>
</ComboBoxItem>
</ComboBox>
Hope this helps! :)

WPF CheckBox with "Button" appearance

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>

Resources