猿问

WPF Listview 绑定不适用于字典

我有一个 WPF 项目,在 XAML 文件中我有一个列表视图并绑定到字典。但是每当我更改字典值的值时,它都不会绑定回 UI。任何人都可以帮忙。


我的代码示例如下所示。


.XAML 文件:


<DockPanel Grid.Row="1" Grid.Column="0">

    <Border BorderBrush="SkyBlue" BorderThickness="1,1,1,1"></Border>

        <StackPanel>

                <ListView Margin="0" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Name="lvAlphaKeys" BorderThickness="0" ItemsSource="{Binding AlphaKeys, Mode=TwoWay}"  >

                    <ListView.ItemTemplate>

                        <DataTemplate>

                            <WrapPanel>

                                <TextBlock Text="{Binding Key}" Width="30" FontWeight="Bold" />

                                <TextBlock Text="{Binding Value.DispalyName, Mode=TwoWay}" />

                            </WrapPanel>

                        </DataTemplate>

                    </ListView.ItemTemplate>

                    </ListView.ItemTemplate>

                </ListView>

        </StackPanel>

</DockPanel>

查看型号:


public class MyViewModel : INotifyPropertyChanged

 {

         public event PropertyChangedEventHandler PropertyChanged;

         private Dictionary<string, MyCommand> _AlphaKeys;


        public Dictionary<string, MyCommand> AlphaKeys

        {

            get { return _AlphaKeys; }

            set

            {

                _AlphaKeys = value;

                OnPropertyChanged(new PropertyChangedEventArgs("AlphaKeys"));

            }

        }

}


 public class  MyCommand

    {

        public string DispalyName { get; set; }

        public string DisplaySymbol { get; set; }

    }

.XaML.CS 文件:


//Field declaration

        MyViewViewModel viewModel;


//Constructur

     viewModel = new MyViewViewModel();

     DataContext = viewModel;   



//Event

    viewModel.AlphaKeys[key].DispalyName = "new value";

如果我设置 itemsource = null 然后重新分配列表值的 Itemsource 它正在工作,否则无法工作,有人可以帮忙吗?lvAlphaKeys.ItemSource = null; lvAlphaKeys..ItemSource = viewModel.AlphaKeys;


森栏
浏览 265回答 1
1回答

ABOUTYOU

您为 设置事件AlphaKeys,如果集合已更改,它将引发一个事件,而不是为他的项目。你必须INotifyPropertyChanged为你的MyCommand,&nbsp;public class&nbsp; MyCommand : INotifyPropertyChanged&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; private string _dispalyName ;&nbsp; &nbsp; &nbsp; &nbsp; public string DispalyName&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get { return _dispalyName ; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _dispalyName = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NotifyOfPropertyChange("DispalyName");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public event PropertyChangedEventHandler PropertyChanged;&nbsp; &nbsp; &nbsp; &nbsp; protected void NotifyOfPropertyChange(string name)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PropertyChangedEventHandler handler = PropertyChanged;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (handler != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handler(this, new PropertyChangedEventArgs(name));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答