1

我正在使用WPF,MVVMDevExpress GridControl. 我的 MainWindow.xaml 中有两个面板。Panle1 有 Grid 而 Panel2 有Textbox. 我希望如果我从 Panel1 的网格中选择一个项目,它的名称应该显示在该 Panle2 文本框中。我写了代码,但它不起作用。你能帮我解决这个问题吗?

*在 Models 文件夹中的 NameModel 中,我写道:*

private NameModelClass _selectedCustomer;
public NameModelClass SelectedCustomer
{
    get { return _selectedCustomer; }
    set
    {
        if (_selectedCustomer != value)
        {
            _selectedCustomer = value;
            LastName = value.LastName;
            OnPropertyChanged("SelectedCustomer");
        }
     }

    public List<Namess> ListPerson { get; set; }

    void CreateList()
    {
        ListPerson = new List<Namess>();
        for (int i = 0; i < 10; i++)
        {
            ListPerson.Add(new Namess(i));
        }
    }

    public class Namess
    {
        public Namess(int i)
        {
            FirstName = "FirstName" + i;
            LastName = "LastName" + i;
            Age = i * 10;
        }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    }
}

在 MianWindow.xaml 我写道:

<dxdo:LayoutPanel Caption="Grid" Caption="Panel1" x:Name="abc1">
    <Grid>
        <dxg:GridControl x:Name="grid" Height="233" ItemsSource="{Binding ListPerson}" AutoGenerateColumns="AddNew" HorizontalAlignment="Left" VerticalAlignment="Top"   SelectedItem="{Binding SelectedNames}">
            <dxg:GridControl.View>
                <dxg:TableView ShowTotalSummary="True"/>
            </dxg:GridControl.View>
        </dxg:GridControl>
    </Grid>
</dxdo:LayoutPanel>

<dxdo:LayoutPanel Caption="Panel2" x:Name="abc1">
    <TextBox Width="166" Background="White" Height="33"  HorizontalAlignment="Right"  VerticalAlignment="Bottom"  Text="{Binding Path=LastName}"/>
</dxdo:LayoutPanel>

我是MVVMC# 的新手。如果您不清楚我的问题,请问我。谢谢你。

4

3 回答 3

1

我这样做:

private Namess _selectedCustomer;
public Namess SelectedCustomer
{
    get { return _selectedCustomer; }
    set
    {
        if (_selectedCustomer != value)
        {
            _selectedCustomer = value;
            OnPropertyChanged("SelectedCustomer");
        }
     }

    public List<Namess> ListPerson { get; set; }

    void CreateList()
    {
        ListPerson = new List<Namess>();
        for (int i = 0; i < 10; i++)
        {
            ListPerson.Add(new Namess(i));
        }
    }

    public class Namess : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        public Namess(int i)
        {
            FirstName = "FirstName" + i;
            LastName = "LastName" + i;
            Age = i * 10;
        }
        public string FirstName { get; set; }
        private string _lastName;
        public string LastName 
        { 
            get
            {
                return _lastName;
            }
            set
            {
                if(value==_lastName)
                    return;
                _lastName=value;
                OnPropertyChanged("LastName");
            }
        }
        public int Age { get; set; }
    }
}

在您看来:

<dxdo:LayoutPanel Caption="Grid" Caption="Panel1" x:Name="abc1">
    <Grid>
        <dxg:GridControl x:Name="grid" Height="233" ItemsSource="{Binding ListPerson}" AutoGenerateColumns="AddNew" HorizontalAlignment="Left" VerticalAlignment="Top" SelectedItem="{Binding SelectedNames,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
            <dxg:GridControl.View>
                <dxg:TableView ShowTotalSummary="True"/>
            </dxg:GridControl.View>
        </dxg:GridControl>
    </Grid>
</dxdo:LayoutPanel>

<dxdo:LayoutPanel Caption="Panel2" x:Name="abc1">
    <TextBox Width="166" Background="White" Height="33"  HorizontalAlignment="Right" VerticalAlignment="Bottom"  Text="{Binding Path=SelectedCustomer.LastName,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/>
</dxdo:LayoutPanel>

Bsally 我将 SelectedCustomer 的类型更改为项目集合之一。在视图中,您可以将 TextBox 的绑定直接设置为 SelectedCustomer 的属性。

于 2013-08-26T21:33:55.730 回答
0

你有没有尝试过:

SelectedItem="{绑定 SelectedNames, Mode=TwoWay}"

仔细研究之后,您的主要 Namess 类可以实现 INotifyPropertyChanged

每个属性都会在它发生变化时引发属性更改事件。

还使用可观察集合,因此当您添加和删除项目时,它也会引发更改。

这样,通知更改系统接收到属性更改的通知,以通过绑定相应地更改视图。

于 2013-08-26T19:02:57.870 回答
0

您似乎忘记为“LastName”字符串引发 INPC (INotifyPropertyChanged) 事件。

所以试试这个(更改在下面的设置器中):

public NameModelClass SelectedCustomer
    {
    get { return _selectedCustomer; }
    set
        {
        if (_selectedCustomer != value)
            {
            _selectedCustomer = value;
            LastName = value.LastName;
            OnPropertyChanged("SelectedCustomer");
            OnPropertyChanged("LastName");   //<-- new 
            }
        }
    }

您必须发送 INPC,以便绑定知道更新为新值。除非您引发该事件,否则显示的绑定不会“获取” LastName 的新值。

于 2013-08-26T19:03:13.740 回答