0

I will start developing for WPF and I have a doubt.

I created a ListView with the Binding property to the next ExtComandaDTO the object. The property in "seleciona" has relationship with a checkbox, but I have following problem. When I click the checkbox it calls the normal event, but when I change the value of "seleciona" at runtime checkbox in my listview is selected but does not call the event check.

There is missing from Listview to be called the event some attribute?

    <ListView x:Name="LvwComanda" Grid.Column="0" 
              Background="{x:Null}"
              Margin="40,36,40,40" 
              SelectedItem="{Binding SelectedExtComanda}" 
              ItemsSource="{Binding ObsExtComanda, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
              Grid.RowSpan="2" >
        <ListView.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Finaliza Comanda" Checked="LvwComandaRowFinalizaComanda_Click" Unchecked="LvwComandaRowFinalizaComanda_Click"></MenuItem>
            </ContextMenu>
        </ListView.ContextMenu>
        <ListView.Resources>
            <Style TargetType="{x:Type ListViewItem}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding finaliza_pendente}"  Value="true">
                        <Setter Property="Foreground" Value="Red" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding finalizada}" Value="true">
                        <Setter Property="Foreground" Value="DarkViolet" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListView.Resources>
        <ListView.View>
            <GridView >
                <GridViewColumn Width="30">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate >
                            <CheckBox Name="ChkComanda" IsChecked="{Binding seleciona.IsChecked, Mode=TwoWay}" Checked="Checked_LvwComandaRow" Unchecked="Unchecked_LvwComandaRow" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Width="Auto" Header="Comanda"      DisplayMemberBinding="{Binding nr_comanda}"/>
                <GridViewColumn Width="Auto" Header="Taxa Servico" DisplayMemberBinding="{Binding taxa_servico}" />
                <GridViewColumn Width="Auto" Header="Finalizada"   DisplayMemberBinding="{Binding finalizada, Converter={StaticResource ReplaceConvertSimNao}}" />
                <GridViewColumn Width="Auto" Header="Observacao"   DisplayMemberBinding="{Binding observacao}"/>
            </GridView>
        </ListView.View>
        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Margin" Value="0,0,0,0"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander IsExpanded="True" BorderBrush="#FFA4B97F" BorderThickness="0,0,0,1">
                                        <Expander.Header>
                                            <DockPanel>
                                                <DockPanel.ContextMenu>
                                                    <ContextMenu Loaded="LvwComandaHeaderContextMenu_Loaded">
                                                        <MenuItem Header="Libera Mesa"  Checked="LvwComandaHeaderLiberaMesa_Click" Unchecked="LvwComandaHeaderLiberaMesa_Click" />
                                                    </ContextMenu>
                                                </DockPanel.ContextMenu>
                                                <CheckBox x:Name="HeaderCheckBox" Checked="Checked_LvwComandaHeader" Unchecked="Unchecked_LvwComandaHeader">
                                                    <StackPanel Orientation="Horizontal">
                                                        <TextBlock FontWeight="Bold" Text="{Binding Name, Converter={StaticResource ReplaceConvertMesaId}}" Margin="5,0,0,0"/>
                                                        <TextBlock Width="Auto" Text=" " />
                                                        <TextBlock FontWeight="Bold" Width="Auto" Text="{Binding Name, Converter={StaticResource ReplaceConvertMesaGrupo}}" />
                                                        <TextBlock Text=" ("/>
                                                        <TextBlock Text="{Binding ItemCount, Converter={StaticResource ReplaceConvertComanda}}"/>
                                                        <TextBlock Text=")"/>
                                                    </StackPanel>
                                                </CheckBox>
                                            </DockPanel>
                                        </Expander.Header>
                                        <ItemsPresenter />
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListView.GroupStyle>
    </ListView>

    #region Event List Row Comanda 

    private void Checked_LvwComandaRow(object sender, RoutedEventArgs e)
    {
        this.Handle_LvwComandaRow((CheckBox)sender, true);
    }

    private void Unchecked_LvwComandaRow(object sender, RoutedEventArgs e)
    {
        this.Handle_LvwComandaRow((CheckBox)sender, false);
    }

    private void Handle_LvwComandaRow(CheckBox sender, bool check)
    {
        if (sender.DataContext is ExtComandaDTO)
        {   
            var row = (ExtComandaDTO)sender.DataContext;

            if (check)
            {
                ObsExtComanda.FindAll(c => c.seleciona && c.id_mesa != row.id_mesa).ForEach(c => c.seleciona = false);
            }

            bool bolComandaSelected = ObsExtComanda.Exists(c => c.seleciona);

            BtPagamento.IsEnabled = bolComandaSelected;
            BtImprimir.IsEnabled  = bolComandaSelected;

            this.PrepareObsPedido(check, row);

            this.PrepareObsComandaPagto(check, row);
        }
    }



public class ExtComandaDTO : ComandaDTO, INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(String property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    private Boolean _seleciona;
    private Boolean _finaliza_pendente;

    public Boolean seleciona
    {
        get { return _seleciona; }
        set { _seleciona = value; OnPropertyChanged("seleciona"); }
    }

    public new Boolean finaliza_pendente
    {
        get { return _finaliza_pendente; }
        set { _finaliza_pendente = value; OnPropertyChanged("finaliza_pendente"); }
    }
}

1 Answer 1

2

Checked and Unchecked are events fired by the UI (not a set).

Handle that stuff in the set if you want to catch changes from code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.