【问题标题】:Some Issue with Two-way Binding - Not working as expected in UWP双向绑定的一些问题 - 在 UWP 中无法按预期工作
【发布时间】:2020-02-04 05:31:48
【问题描述】:

问题:通过 ComboBox cb1 更改位置时,相关位置 TextBlock 不会更改为更新值。

我正在自学,下面是关于绑定的实验代码 公共 EmpDeptViewModel 虚拟机;它在按钮单击事件上初始化如下

private void btn2_Click(object sender, RoutedEventArgs e) { 
vm = new EmpDeptViewModel();
this.Bindings.Update();  }

XAML 看起来像这样。

 <ListView x:Name="listview3" ItemsSource="{x:Bind vm.InstanceOfDepartmentData}">
                        <ListView.ItemTemplate>
                            <DataTemplate x:DataType="classes:Department">
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Margin="5">
                                            <Run Text="DeptNo: " /><Run Text="{x:Bind DeptNo}" />
                                    </TextBlock>
                                    <TextBlock Margin="5">
                                            <Run Text="DeptName: " /><Run Text="{x:Bind DeptName}" />
                                    </TextBlock>

                                    <TextBlock Margin="5">
                                            <Run Text="Location: " /><Run Text="{x:Bind Location, Mode=OneWay}" />
                                    </TextBlock>

                                    <ComboBox  x:Name="cb1" ItemsSource="{Binding Source={StaticResource MyLocatonList}, Path=ListofLocationsInsideViewModel, Mode=TwoWay}" DisplayMemberPath="LocationName" SelectedValuePath="LocationName"  SelectedValue="{x:Bind Location}"  />
  </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

问题:通过 ComboBox cb1 更改位置时,相关位置 TextBlock 不会更改为更新值。

x:DataType="classes:Department" 看起来像这样。

 public class Department : BindableBase
    {   private string _location;
        public Department(int pdeptNo, string pdeptName, string plocation)
        {
            DeptNo = pdeptNo;
            DeptName = pdeptName;
            Location = plocation;
            ListOfDeparmentEmployees = new List<Employee>();    }

        public int DeptNo { get; set; }
        public string DeptName { get; set; }
        public string Location {
            get { return this._location; }
            set { this.SetProperty(ref this._location, value); }
        }
        public List<Employee> ListOfDeparmentEmployees { get; set; }


    }

【问题讨论】:

    标签: c# uwp binding


    【解决方案1】:

    你可能被绑在了错误的位置

    在 ComboBox 中,将 TwoWay 设置为 ItemsSource。这根本不符合逻辑。如果修改 ComboBox 的值,则无法更改 Location

    试试这个:

    Xaml

    ...
    <ComboBox  x:Name="cb1" ItemsSource="{Binding Source={StaticResource MyLocatonList}, Path=ListofLocationsInsideViewModel}" 
               DisplayMemberPath="LocationName" SelectedValuePath="LocationName"  SelectedValue="{x:Bind Location,Mode=TwoWay}"  />
    ...
    

    但是,如果直接写,会导致死循环,然后报错。您需要重写Department 类的Location 属性。

    Department.cs

    ...
    public string Location
    {
        get { return this._location; }
        set 
        {
            if (_location != value)
            {
                this.SetProperty(ref this._location, value);
            }
        }
    }
    ...
    

    另外请注意你的BindableBase基类是否实现了INotifyPropertyChanged接口,这是在修改数据的同时修改UI的基础。

    最好的问候。

    【讨论】:

    • 非常感谢 Richard Zhang。解决了。感谢您提供“无限循环”错误提示。
    猜你喜欢
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 2021-04-23
    • 2014-11-19
    • 2012-01-16
    • 2015-11-16
    相关资源
    最近更新 更多