Reputation: 1125
I have a problem with a ListView in WPF. My program has two ListView control: one that uses data binding to bind with an ObservableCollection which is inside my Window, and one that has a binding to an ObservableCollection inside every item of the first collection.
Like this:
public class Fitosanitario : DBData
{
private ObservableCollection<Dosaggio> _dosaggi;
public ObservableCollection<Dosaggio> Dosaggi { set { _dosaggi = value; } get { return _dosaggi; } }
...
}
public class Dosaggio : DBData
{
private int _idColtura;
public int IDColtura
{
get { return _idColtura; }
set { _idColtura = value; }
}
...
}
And the two lists are defined like this:
ObservableCollection inside my Window that the first ListView binds the data to
private ObservableCollection<Fitosanitario> _fitosanitari;
public ObservableCollection<Fitosanitario> Fitosanitari
{
get { return _fitosanitari; }
}
XAML for the first ListView
<ListView Grid.Row="1" Margin="5" Name="lstProdottiFito">
<ListView.View>
<GridView>
<GridViewColumn Header="Nome" Width="120" DisplayMemberBinding="{Binding Nome}" />
<GridViewColumn Header="Quantità" Width="80" DisplayMemberBinding="{Binding Quantita}" />
<GridViewColumn Header="Descrizione" Width="135" DisplayMemberBinding="{Binding Descrizione}" />
</GridView>
</ListView.View>
</ListView>
XAML for the second ListView
<ListView Grid.Column="2" Margin="5" ItemsSource="{Binding SelectedItem.Dosaggi, ElementName=lstProdottiFito}" x:Name="lstDosaggi">
...
</ListView>
Everything works fine for now. My problem is that the second ListView, has to be editable, and in order to do so I've customized its CellTemplate for eact GridColumn (with some trigger to enable and disable editing) in this way (this is the example for the first column, but thay're all equal):
<GridViewColumn Header="IDColtura" Width="80">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ContentControl>
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding InEditing}" Value="False">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding IDColtura}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding InEditing}" Value="True">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBox Text="{Binding IDColtura, Mode=OneWay}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
And the switching from "editing mode" and "normal mode" works, the problem is the neither the TextBox or the TextBox displays the text. I've tried every type of DataBinding but i couldn't get it to work.
Does anyone have an idea?
I apologize I've been so long but I didn't know how to explain it.
Thank to everyone! :)
Upvotes: 0
Views: 1240
Reputation: 69989
You've totally over complicated the situation. Just define the DataTemplate
for your Dosaggio
class like this:
<DataTemplate DataType="{x:Type Dosaggio}">
<TextBox Text="{Binding IDColtura}" IsReadOnly="{Binding IsNotEditing}" />
</DataTemplate>
You're better off using the TextBox.IsReadOnly
property to make a TextBox
uneditable, but you'd need to reverse the polarity of your InEditing
property to make it work and maybe rename it to IsNotEditing
. Also, if this property is not in your Dosaggio
class, then you will have to change the Binding
to access it.
For your future reference:
You don't need to use two DataTrigger
s to do what you were doing in your example. You could have done this instead:
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding IDColtura}"/>
</DataTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding InEditing}" Value="True">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBox Text="{Binding IDColtura, Mode=OneWay}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
UPADTE >>>
If you're going to use a ContentControl
, then you need to set its Content
property:
<ContentControl Content="{Binding}">
...
</ContentControl>
Upvotes: 1