组合框selectedValue和重写ToString()-MVVM WPF

时间:2019-04-24 09:49:44

标签: c# wpf xaml combobox datagrid

我有一个组合框,它显示来自重写ToString()的数据。我还有一个datagrid文本列,它也从重写ToString()中获取其值。

我想要的是将组合框同步到datagrid,以便当我们单击datagird行时,它在组合框中显示相同的值,而当我们更改组合框中的值时,它将更新datagrid单元。问题是我似乎无法弄清楚如何使用ToString()。当我将其绑定到具有getter和setter的属性时,它将起作用。但是我需要将它放在ToString()上。

型号

  public class SifKorisnici: INotifyPropertyChanged
    {
        private string _ime;
        private string _prezime;

        public string Ime
        {
            get { return _ime; }
            set
            {
                _ime = value;
                OnPropertyChanged("Ime");
            }
        }

        public string Prezime
        {
            get { return _prezime; }
            set
            {
                _prezime = value;
                OnPropertyChanged("Prezime");
            }
        }

        public override string ToString()
        {
            return Prezime + " " + Ime;
        }
    }

Datagrid

  <DataGrid ItemsSource="{Binding ListaOdjeli,Mode =TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                  AutoGenerateColumns="False" CanUserAddRows="False" Margin="0,180,0,0"
                  IsReadOnly="True"
                  IsSynchronizedWithCurrentItem="True"
                  SelectedItem="{Binding SelectedItems,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
            <DataGrid.Columns>

                <DataGridTextColumn Header="Voditelj" Binding="{Binding SifKorisnici,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/>
        </DataGrid>

组合框

  <ComboBox Name="OdaberiKorisnika" ItemsSource="{Binding ListaKorisnikaCombo, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                  HorizontalAlignment="Left" Margin="111,142,0,0" VerticalAlignment="Top" 
                  Width="120" 
                  IsReadOnly="True"
                  IsSynchronizedWithCurrentItem="True"
                  SelectedItem="{Binding SelectedKorisnici,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                  SelectedValue="{Binding SelectedItems.SifKorisnici??,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                  SelectedValuePath="SifKorisnici??"

                  IsEnabled="{Binding JeOmogucenGumb}"
        </ComboBox>

0 个答案:

没有答案