Delete datagrid column programmatically - wpf

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.

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?

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>

SelectionChanged event not raised

Is there a way to fix the known bug of Selection Change Event, selecting does not work if the same item is tapped again.
to give further background, my scenario is that I have four items in my pivot page and when I click one of those items i will be navigated to another page. Now my dilemma is that when i select the same items again, navigation does not work or nothing happens.
Please let me know your suggested fix, thanks much in advance.
<ListBox x:Name="lbviewlist" ItemsSource="{Binding items}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="selectionchanged">
<Command:EventToCommand Command ="{Binding ItemListCommand }" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock TextWrapping="Wrap" Text="{Binding itemName}" FontSize="30" Margin="10,0,0,0" Style="{StaticResource PhoneTextTitle2Style}" Foreground="CadetBlue"/>
<TextBlock TextWrapping="Wrap" Text="{Binding itemDescription}" FontSize="20" Margin="15,5,0,10"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
A good alternative approach is to include all of the content in each item inside a button, which is styled to be invisible so that you never see it, but it covers the surface of the item it encapsulates. If you're using MVVM you can bind the same command property to each of your 'buttons' and bind the DataContext of the button (your data item) to the command parameter. Then whenever you click an item you'll get the command firing every time.
You might want to change your items control to something simple so that the selection changed events dont get in the way.

How do I get the corresponding data item when someone clicks a button within my data template WPF?

I am new in WPF and i need to make and application to show all the programs installed in windows. I found this layouts and i started to work in the fisheye panel, but the problem is that i want to show buttons instead of images in the layout, so I started to figure it out how to do that and i get to show buttons instead of pictures with the next code:
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Button Margin="5" Padding="5" HorizontalContentAlignment="Center"
Command="{Binding Command}" Width="{Binding XPath=#Width}"
Height="{Binding XPath=#Height}" Content="App"/>
</DataTemplate>
</Setter.Value>
</Setter>
My problem is: How do i bind every button to the corresponding action? and how can i know which one was pressed or the id of the button?
Refer the following,
http://www.actiprosoftware.com/community/thread/3513/using-button-in-datatemplate-binding-command
You need to use TemplatedParent in my mind. The TemplatedParent property gives you an item which this template is applied. Use this code
<Button Margin="5" Padding="5" HorizontalContentAlignment="Center"
Command="{Binding RelativeSource={RelativeSource Mode=TemplatedParent} , Path=Command}"
Width="{Binding XPath=#Width}" Height="{Binding XPath=#Height}"
Content="App"/>

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.

Resources