Bind Popup on a Button in a GridView Column - c#

I've a little Problem. On my Window, i've a GridView and on every Row of the GridView is a Button (The Button is Part of the GridViewColumn.CellTemplate). Now i want, that when i press the Button, a Popup should be open under the pressed Button.
But how i can bind the Popup on the Button in a Grid because the Button is dynamic (for every Row one Button).
<Popup Name="popup_Zuordnungen">
<controls:Anlagenzuordnung Grid.Row="3" x:Name="VertragsAnlagenPopup" Margin="0,20,0,0"> </controls:Anlagenzuordnung>
</Popup>
<ListView Grid.Row="1" Name="lv_Leistungserbringer" DataContextChanged="lv_Leistungserbringer_DataContextChanged" SelectionChanged="lv_Leistungserbringer_SelectionChanged" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:VertragsLeistungserbringerZuordnung}}, Path=DataSource}" Height="150" VerticalAlignment="Bottom">
<ListView.View>
<GridView>
<GridViewColumn Header="ID">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ID}" Width="40" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Bezeichnung">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Beschreibung}" Width="500" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Zuordnungen anzeigen" Name="cmd_Zuordnungen"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
The Button is in the last Column.
Cab anywhere help me?

This is a Window with code-behind?
In that case, you can simply add a click event to your button (in the DataTemplate), and handle the popup placement in the event handler:
XAML:
...
<DataTemplate>
<Button Content="Zuordnungen anzeigen" Name="cmd_Zuordnungen"
Click="cmd_Zuordnungen_Click" />
</DataTemplate>
Code:
private void cmd_Zuordnungen_Click(object sender, RoutedEventArgs e)
{
popup_Zuordnungen.IsOpen = false;
popup_Zuordnungen.PlacementTarget = (Button)sender;
popup_Zuordnungen.IsOpen = true;
}

Related

Binding some checkbox into combobox to a listView

I have an establishment collection that I display in a combobox. One of the properties is "IsSelected", which allows me to select multiple items in a combobox.
<ComboBox Name="CmbEtabTout"
ItemsSource="{Binding EtablissementsUtilisateur}"
Grid.IsSharedSizeScope="True"
Grid.Column="2"
Grid.ColumnSpan="3"
Grid.Row="2"
Height="25"
Width="250">
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30" />
<ColumnDefinition SharedSizeGroup="AgentA" Width="auto" />
<ColumnDefinition Width="5" />
<ColumnDefinition SharedSizeGroup="AgentB" Width="auto" />
</Grid.ColumnDefinitions>
<CheckBox IsChecked="{Binding IsSelected}" Grid.Column="0"/>
<TextBlock Text="{Binding IdEtablissement}" Grid.Column="1"/>
<TextBlock Text="{Binding Nom}" Grid.Column="3"/>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ListView x:Name="LVAgent"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding Agents}" Grid.ColumnSpan="2" Margin="150,0,42,0" Grid.Column="2" Grid.Row="4" Grid.RowSpan="5" >
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected}"
Command="{Binding DataContext.SelectAgentCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"
CommandParameter="{Binding}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
<CheckBox IsChecked="{Binding SelectAllAgents}"
IsEnabled="True"/>
</GridViewColumn>
<GridViewColumn Header="Matricule"
Width="110"
DisplayMemberBinding="{Binding Matricule}"/>
<GridViewColumn Header="Nom"
Width="120"
DisplayMemberBinding="{Binding Nom}"/>
<GridViewColumn Header="Prénom"
Width="120"
DisplayMemberBinding="{Binding Prenom}"/>
</GridView>
</ListView.View>
</ListView>
My combobox collection :
private ObservableCollection<Etablissement> _EtablissementsUtilisateur;
public ObservableCollection<Etablissement> EtablissementsUtilisateur
{
get
{
return _EtablissementsUtilisateur;
}
set
{
if (value != _EtablissementsUtilisateur)
{
_EtablissementsUtilisateur = value;
RaisePropertyChanged(nameof(EtablissementsUtilisateur));
}
}
}
I'm trying to find out how to bind these comboboxes to refresh a list: if I choose three establishments, the list displays the agents of these three establishments. With a command maybe?
<CheckBox IsChecked="{Binding IsSelected}" Grid.Column="0"/>
Look like this :
Since I already bind my checkbox with "IsSelected" (used for SelectAll), I don't know how to bind to refresh the list of Agent behind, when I check, without having to press a button like "validate".
Edit : My problem now is if I want to do something like this, for exemple :
<CheckBox IsChecked="{Binding IsSelected}" Command="{Binding }" Grid.Column="0" />
I can only bind to the Etablissement Class and not to the ViewModel. (because of the itemSource of combobox i think)
the goal is, "when any checkbox is checked or unchecked, if I choose three establishments, the list displays the agents of these three establishments".
"when any checkbox is checked or unchecked" => property changed event handler
"if I choose three establishments" => if statement
"the list displays the agents of these three establishments" => method call
assuming Etablissement : INotifyPropertyChanged, we could add the event handler to each Etablissement.PropertyChanged. the other option is to add the handler to CheckBox.Checked and CheckBox.Unchecked.
bonus: event handlers can be async, so if "method call" is async, you're right at home to await it, meaning your UI remains responsive and does not lock up.
i would add the handlers inside a Loaded event for your UserControl or perhaps the ComboBox
Loaded += delegate
{
PropertyChangedEventHandler propertyChanged = delegate
{
//if number of checked items != 3
//return;
//update agents
};
foreach (var etablissement in EtablissementsUtilisateur)
etablissement.PropertyChanged += propertyChanged;
}

Selected checkboxes in datagrid C# WPF get copied in other checkboxes

I have this problem with my C# WPF app:
I have the checkboxes on every row but when I select (random) one or more checkboxes I noticed that other checkboxes on other rows (when I scroll to see the other rows) are selected, too. It's like there would be a function or something that checks automatically other checkbox different of those selected by me.
There is no C# code for the checkboxes (just the definitions of the functions for Checked, Unchecked). I use a query and ItemSource to fill the Datagrid.
So, is there a way to avoid the automatic selection of other checkboxes ?
Thankx
My XAML code:
<DataGridTemplateColumn x:Name="colonnaChiusoF" Header="Cf" Width="26">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=sc1, Mode=TwoWay}" Checked="checkedF" Unchecked="uncheckedF" Margin="0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Actually it happens with other objects, too. For example, in this code the buttons and checkboxes are selected automatically random if I select/deselect some of them:
<ListView Name="listaView" ItemsSource="{Binding Path=Table}" Height="200" Width="400" ScrollViewer.VerticalScrollBarVisibility="Visible" IsSynchronizedWithCurrentItem="True" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}">
<ListView.View>
<GridView>
<GridViewColumn Header="Idprev" DisplayMemberBinding="{Binding Path=idPrev}" />
<GridViewColumn Header="DataPrev" DisplayMemberBinding="{Binding Path=dataPrev}" />
<!--<GridViewColumn Header="Test3" DisplayMemberBinding="{Binding Test3}" /> -->
<GridViewColumn Header="Chiudi">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Margin="0" BorderThickness="0" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Button">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ToggleButton IsChecked="True">Button Text</ToggleButton>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
The C# code:
DbDataAdapter da = daoObject.list_3(0, 0, arrayFilter);
DataSet dataset = new DataSet();
da.Fill(dataset);
listaView.DataContext = dataset.Tables[0].DefaultView;
Well, the problem should be the virtualization. If I UNCHECK "Enable row virtualization" it seems the checkboxes remain as I select them so there is no random automatically check-uncheck.
The problem now is that the records are loaded slower than with the virtualization enabled.

C# - WPF - Access TextBox inside of a listview?

I have a listview with three columns, When I select a listview item, how can I access the TextBox of the selected item?
In my case I would like to focus the TextBox "textQuantity" when I select an item.
<ListView x:Name="EntryListView" Height="Auto"
ItemsSource="{Binding TheList}"
MouseDoubleClick="EntryListView_MouseDoubleClick"
SelectionChanged="EntryListView_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="Quantity">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Name="textQuantity" Text="{Binding DefaultQuantity}" Width="40" IsTabStop="True"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Block1">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Block1}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Block2" Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Block2}"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I Guess I need to do something in Selectionchanged?
private void EntryListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
UPDATE:
Just trying to explain more:
When I select the first row of the listview, by tabing into it, or selecting the row by left click. I want to be able to write into the Quantity-TextBox of the selected ListView item.
For example, If I press on Test1, I want to directly be able to write into the field where the text is 100. Now I have to press inside the TextBox to be able to edit the value.
You're binding the item source to a collection, create a property of the same type as your collection. Then bond your listbox selecteditem to that property.
Alternatively bind to a selected index property.
Either gives you a way to identify which item in the collection is selected.

Remove ListItem from nested ListView on Button Click

I have a nested Listbox (collection of objects in the main object list) that needs to delete the underlying items.
When the item is removed, I could reset the ItemsSource of the main list, but the main list will have tons of items and each time a item is removed from its underlying collection the main scrollbar would be also reset, making users willing to kill me in a very painful way.
My question: How can i find the container of the item which button has been clicked and how can i find the item itself so i can kill the #&!$*&#$# (cursing onomatopoeia)?
Here's the XAML exemple of my lists:
<ListView Name="mainList">
<ListView.View>
<GridView>
<GridViewColumn Header="Column 1" />
<GridViewColumn Header="Column 2" />
<GridViewColumn Header="Column 3" />
<GridViewColumn Header="Collection column">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding BindingCollectionProperty}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Grid.Column="0" Text="{Binding Item.Property}" />
<TextBlock Grid.Column="1" Text="{Binding Item.AnotherProperty}" />
<Button Content="remove" Grid.Column="2" Click="DeleteClickEvent" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
How's my DeleteClickEvent(object sender, RoutedEventArgs e) should be like?
You could have a command and pass it your item as parameter instead of a click handler:
<Button ... Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type YOURCUSTOMCONTROL}}, Path=DataContext.YOURCOMMAND}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" />
If you want to keep your event handler you could use VisualTreeHelper.GetParent twice on the sender.
ListBoxItem item = VisualTreeHelper.GetParent(VisualTreeHelper.GetParent((DependencyObject)sender)) As ListBoxItem;
BindingCollectionProperty.Remove(item);

How to access element (Combo Box) in XAML from code behind

How do I access a control in XAML that is nested in a GridViewColumn.CellTemplate? By accessing the combo box I want to set its ItemsSource in the code behind.
Code:
<GridViewColumn Width="80">
<GridViewColumnHeader Content="UseCLUT"/>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=UseCLUT}" Style="{StaticResource GridBlockStyle}"/>
<ComboBox x:Name="combTrueFalse" SelectedItem="{Binding Path=UseCLUT}" Style="{StaticResource GridEditStyle}" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
I have named the comboBox as combTrueFalse and tried to referenced it in the code behind but it could not be found.
I find a work around to the problem.
I have set the collection of the combo box to a class which contains the collection for the combox's selection.
The mainWindow class contains the data class's variable.
ItemsSource="{Binding ElementName=mainWindow, Path=data.comboxTFSmall}"
Meaning to say I have set the combox's item towards a Class which contains the collection and not from the code behind.
<GridViewColumn Width="80" >
<GridViewColumnHeader Content="UseCLUT"/>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=UseCLUT, Mode=TwoWay}" Style="{StaticResource GridBlockStyle}"/>
<ComboBox ItemsSource="{Binding ElementName=mainWindow, Path=data.comboxTFSmall}" SelectedValue="{Binding Path=UseCLUT}" Style="{StaticResource GridEditStyle}" />
</Grid>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
Please do correct me if you find that my explanation is misleading thanks.
regards

Categories

Resources