Bind problem in C# WPF

Asked

Viewed 101 times

0

I have a WPF that is as follows:

 <ComboBox Name="comboBoxRemetente" VerticalContentAlignment="Center" Margin="5" Grid.ColumnSpan="20"
              ItemsSource ="{Binding Path=Lista, Mode = TwoWay}" 
              SelectedValue="{Binding Path=Model, Mode = TwoWay}"
              DisplayMemberPath="Remetente"
              IsSynchronizedWithCurrentItem="True"
              SelectedIndex="0"
    >
    </ComboBox>
<DataGrid Name="GridLista" 
                     Grid.ColumnSpan="20" 
                     Margin="5" 
                     Grid.RowSpan="11" 
                     Grid.Row="8"
                     CanUserAddRows="False"
                     CanUserReorderColumns="True"
                     AutoGenerateColumns="False" 
                     SelectionUnit="FullRow"
                     SelectionMode="Extended"
                     ItemsSource="{Binding Path=Lista}"
                     SelectedValue="{Binding Path=Model, Mode = TwoWay}"
                     IsReadOnly="True"
              >

So, when I start the screen I load the FATHER list and it fills the fields almost everything correctly. My problem is:

I got this combobox:

<ComboBox Name="comboBoxCartao" Grid.Row="7" Grid.ColumnSpan="8" VerticalContentAlignment="Center" Padding="0" Margin="5" Grid.Column="2"
              ItemsSource ="{Binding Path=CartaoBean.Lista, Mode = TwoWay}" 
              SelectedValue="{Binding Path=Model.Cartao, Mode = TwoWay}"
              DisplayMemberPath="ToComboBox"
              IsSynchronizedWithCurrentItem="True"
              SelectedIndex="0"
    >
    </ComboBox>

This last combobox carries a list of CHILDREN, I have set this one a son, so I need that when selecting a FATHER, he also select the SON in this combobox, could you help me where I am missing? rs

Hash Method of the Card:

public override bool Equals(object obj)
    {
        var cart = obj as CartaoPostagemModel;
        if (cart == null) return false;
        return cart.Cartao == this.Cartao;
    }

    public override int GetHashCode()
    {
        unchecked // Overflow is fine, just wrap
        {
            int hash = 17;
            // Suitable nullity checks etc, of course :)
            hash = hash * 23 + Id.GetHashCode();
            hash = hash * 23 + Cartao.GetHashCode();
            hash = hash * 23 + Contrato.GetHashCode();
            return hash;
        }
    }
  • Can you improve and/or contextualize your question? What you want is to select a standard item for the Card combo when the main item (X) is changed?

  • I have a combobox that you select the FATHER(sender) within the FATHER has a SON(card), I have another combobox that has a list of SON(card) so my wish is: when I select the FATHER he sees the list of son, and selects also, I was able to explain? rs

  • So you have a combobox with a list of cards and want some to be selected according to the chosen sender right? For example, the combobox has 10 cards, but the patch has only 3 registered. When selecting the patch, you want the 3 selected?

  • almost that, in the case of this example, the combobox has 10 cards, and need among the 10 registered cards, be selected only 1 card that is registered inside the Sender, now was? rs

1 answer

0

Apparently, your problem is because of the use of Binding by object. C# (not a WPF issue) does not know when two objects are equal if they are not the same instance. To solve this, you can:

1 - Overwrite the Equals method

class Cartao {

        public override bool Equals(object obj)
        {
            var cart = obj as Cartao;

            if (cart == null) return false;
            return cart.Numero == this.Numero;

        }
}

2 - Swap Binding for a key field. Well, in this case it is a little more work, because you need to use the key field and also change the instance, if you use instance in the Rementente class, whenever the option is exchanged:

XAML

<ComboBox Name="comboBoxCartao" Grid.Row="7" Grid.ColumnSpan="8" VerticalContentAlignment="Center" Padding="0" Margin="5" Grid.Column="2"
              ItemsSource ="{Binding Path=CartaoBean.Lista, Mode = TwoWay}"              
              SelectedValuePath="Cartao_Id"           
              SelectedItem="{Binding Path=Model.CartaoView, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
              SelectedValue="{Binding Path=Model.Cartao_Id, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
              DisplayMemberPath="ToComboBox"
              IsSynchronizedWithCurrentItem="True"
              SelectedIndex="0">
</ComboBox>       

C#

private CartaoViewData CartaoView;

public CartaoViewData CartaoView
{
    get { return CartaoView; }
    set
    {
        CartaoView = value;
        base.OnPropertyChanged(nameof(CartaoView));

        if (value == null)
        {
            Cartao_Id = null;
        }
        else
        {
            Cartao_Id = value.Id;
        }

    }
}

public int? Cartao_Id
{
    get { return _CartaoId; }
    set
    {
        _CartaoId = value;
        base.OnPropertyChanged(nameof(Cartao_Id));
    }
}
  • if I talk nonsense please correct me, but from what I understand Multiselect Combobox is to select varios at the same time, in my case I need to be selected only one, not a list is like selecting the first item after loading the list, however in my case I have to select an item that is in a certain position, in my case it is more for a single select

  • You’re right, I didn’t pay attention to the phrase "only". I updated my answer

  • so I did the most practical thing you told me, but he still doesn’t select, there’s something else wrong with what I did?

  • If there is no Binding error (this you check in the Output window when you change the selection), you may need to implement the Inotifypropertychanged interface, and notify when the Model property is changed. This way the WPF will know that you have changed (selected) the sender and trigger your screen update flow (the Twoway in your class if you implement this interface). Take a look at the example https://docs.microsoft.com/pt-br/dotnet/framework/wpf/data/how-to-implement-property-change-notification

Browser other questions tagged

You are not signed in. Login or sign up in order to post.