Button Visibilty inside a Datagrid - c#

i've created a Datagrid Control in WPF. how can i make my Button visible only for a Cell
inside the row that i have select it.
anyone can help.
XAML:
<DataGridTemplateColumn x:Name="typ" Header="H." Width="50">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="btnTabelle" Visibility="Hidden" Height="20" Width="25"
Click="Button_Table_Click">
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

You can set the Visibility based on if the DataGridCell.IsSelected, although you will need a BooleanToVisibilityConverter to convert the boolean value to a Visibility one
Here's an example, using a RelativeSource binding to find the DataGridCell
<Button Visibility="{Binding Path=IsSelected,
RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}},
Converter={StaticResource MyBooleanToVisibiltyConverter}" ... />

Related

Set Binding of DataGridTextColumn's TextBlock

I'm trying to change the binding for the "Text" property for a TextBox inside of a DataGrid. Consider the following code:
<DataGrid Name="CoreView837" HorizontalAlignment="Left" Height="366" VerticalAlignment="Center" Width="792"
AutoGenerateColumns="False" SelectionUnit="Cell" SelectionMode="Extended" VerticalScrollBarVisibility="Auto" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn x:Name="ClaimNumber" Header="Claim Number" Width="350" Binding="{Binding ClaimNumber}">
</DataGridTextColumn>
<DataGridTemplateColumn x:Name ="Parent837" Header="837 Segment" Width="300" ClipboardContentBinding="{Binding SegmentText}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ScrollViewer MaxHeight="100">
<TextBlock x:Name="Segment837" Text="{Binding SegmentText}" TextWrapping="Wrap"/>
</ScrollViewer>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Errors" Width="350" ClipboardContentBinding="{Binding ErrorText}">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ScrollViewer MaxHeight="100">
<TextBlock Text="{Binding ErrorText}" TextWrapping="Wrap"/>
</ScrollViewer>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
I want to change the binding of the TextBlock "Segment837" in the code behind to a new binding, like below:
Segment837.Binding = new Binding("Value");
However, it seems that I can't change the Text binding of the TextBlock directly. It seems I can only select "Parent837."
Can anyone suggest to me how to change the binding either directly or even a better way? Right now I have the binding set for when a text file's broken segments are successfully loaded, but I wanted to change the binding of the TextBlock for instance in the case that it fails and I want to return alternative data.

DataGrid: How do I programmatically select a ComboBox item?

The ComboBoxes in question are inside a DataGrid's column and have been defined as follows:
<DataGridTemplateColumn Header=" Right Column ">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Caliber, RelativeSource={RelativeSource AncestorType=Window}}" DisplayMemberPath="Thicknesss" SelectedItem="{Binding Thickness, UpdateSourceTrigger=PropertyChanged" SelectionChanged="ComboBox_SelectionChanged">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
TIA
Probably you are looking for DataTriggers which can be set on Style of the ComboBox and depending on the condition the ComboBox can be enabled or disabled.
The ComboBoxes in question are inside a DataGrid's column and have been defined as follows:
<DataGridTemplateColumn Header=" Right Column ">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Caliber, RelativeSource={RelativeSource AncestorType=Window}, Mode=TwoWay}" DisplayMemberPath="Thickness" SelectedItem="{Binding Thickness, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" >
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Link
http://social.technet.microsoft.com/wiki/contents/articles/19493.wpf-best-combobox-tutorial-ever.aspx#DataGridTemplateColumn_CellTemplate

Filter WPF DataGrid values from TextBox in Column Header

I followed the advice in the following link (filter wpf datagrid values from a textbox) to create Figure A. Which allows me to filter the DataGrid based on what is in the TextBox when I press the Button.
However once I move the Button and TextBox within the column headers of the DataGrid like in Figure B. The Button still works but the Binding of the TextBox doesn't as it is showing up null within the code.
http://i42.tinypic.com/2quivpx.png
The only real change between what was done in the link is my XAML where I have put my TextBox and Button within the DataGridTemplateColumn Header.
<Grid DataContext="{Binding ElementName=UI}" >
<DataGrid AutoGenerateColumns="False" Height="184" HorizontalAlignment="Left" Margin="12,23,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="384" ItemsSource="{Binding DataGridCollection}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Name="IDSearcBox" Text="{Binding FilterString}" Width="100"/>
<Button Name="IDSearchButton" Click="IDButton_Click" Content="I" Width="30"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ID}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Name="NameSearchBox" Text="{Binding FilterString}" Width="100"/>
<Button Name="NameSearchButton" Click="NameSearchButton_Click" Content="N" Width="30"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="SomethingElse" Binding="{Binding SomethingElse}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
Any assistance on this would be greatly appreciated! I've been scratching my brain all day on this one!
(I would have posted on the other post for some advice but I couldn't see any option!)
The Textbox is now inside the DataGrids ItemSource DataContext,
Try setting the DataContext of the TextBox back you your out to the UI DataContext
<TextBox Name="IDSearcBox" Text="{Binding FilterString, ElementName=UI}"

CommandParameter multibinding to return DataGrid control and the custom object in the selected row

I'm using .Net 4.0 and have a DataGrid in a view. I have implemeneted this http://www.codeproject.com/Articles/42227/Automatic-WPF-Toolkit-DataGrid-Filtering to provide filtering. The ItemsSource for the DataGrid is an observable collection of my custom objects.
Each row has a Button that when clicked, passes the selected custom object back via the CommandParameter.
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding Path=DataContext.DeleteMyCustomObjectCommand,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}}}" CommandParameter="{Binding}" Width="20">
<Image Source="/MyThing;component/Images/delete.png" Height="16" Width="16"/>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
I want to be able to save the filter criteria using this solution http://www.codeproject.com/Articles/42227/Automatic-WPF-Toolkit-DataGrid-Filtering?msg=3342202#xx3342202xx but one of the calls requires a reference to the data grid
QueryController queryController= DataGridExtensions.GetDataGridFilterQueryController(myGrid1);
As i'm using MVVM, I don't have a reference to the DataGrid in my ViewModel. My command execution code (in the ViewModel) looks like this at present
public void DeleteMyCustomObject(object param)
{
MyCustomObject m = param as MyCustomObject;
.....Deletion commands go here
Is there a way I can use multibindings on the CommandParameter of my Delete button to pass back the custom object from the current row, and a reference to the actual DataGrid (or is there a better solution).
Many Thanks
Mick
(1) bound the DataGrid.SelectedItem to a Property in you ViewModel .
(2) send the Grid as the CommandParameter .
<DataGrid Grid.Column="2" Name="DG1" ItemsSource="{Binding}" SelectedItem="{Binding SelectedItem}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding Path=DataContext.DeleteMyCustomObjectCommand,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding RelativeSource=
{RelativeSource FindAncestor,
AncestorType={x:Type DataGrid}}}" Width="20">
<Image Source="/MyThing;component/Images/delete.png" Height="16" Width="16"/>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn>
<DataGrid.Columns>
</DataGrid>

How to bind to the ItemsSource of a DataDrid from inside DataRow?

I'm working with a window with several DataGrid's, and I'd like to process deletion event via a single command.
For this, I need to pass to that command the list of records, from which the record has to be deleted.
Here's what I mean:
<DataGrid Margin="0" HeadersVisibility="None"
ItemsSource="{Binding GroupExtednedDataList}"
... >
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="delete"
CommandParameter="{Binding:::
How do I bind from here to GroupExtednedDataList from ItemsSoruce?}" >
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
How do I bind from inside <Button Content="delete" to ItemsSource of the DataGrid?
Something like
{Binding ItemsSource, RelativeSource={RelativeSource AncestorType=DataGrid}}

Categories