1
我在运行时将我的ListBox绑定到ObservableCollection。在单击按钮时,我的集合中的某个项目会被修改,但相应的ListBox项目不会相应地自行更新。我已经通过几个类似的SO文章和其他帮助材料,似乎我正在做他们所要求的一切,但没有运气。似乎一切正常加载和绑定,但当我更改我的集合中项目的“IsLoading”属性时,绑定到IsLoading属性的网格的可见性(请参阅下面的DataTemplate)不会更改。WPF ListBox:绑定到ObservableCollection
以下是我的列表框XAML:
<ListBox Name="lstItems">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Name="ListBoxGrid">
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" IsChecked="{Binding IsSelected}" />
<Image Grid.Column="1" Width="50" Stretch="Uniform" Source="{Binding Image}" />
<TextBlock Grid.Column="2" Text="{Binding Path=ImageFilePath}" />
<Grid Grid.Column="3" Visibility="{Binding IsLoading, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, Mode=TwoWay, BindsDirectlyToSource=True, Converter={StaticResource BooleanToVisibilityConverter1}}">
<my:LoadingAnimation x:Name="SendAnimation" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是我的BO:
public class Order : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public bool IsSelected { get; set; }
public string ImageFilePath { get; set; }
public ImageSource Image { get; set; }
private bool mIsSending = false;
public bool IsSending
{
get { return mIsSending; }
set
{
mIsSending = value;
if (PropertyChanged != null)
PropertyChanged(null, new PropertyChangedEventArgs("IsSending"));
}
}
}
这是我如何创建集合,并将其绑定:
ObservableCollection<Order> mOrders = new ObservableCollection<Order>();
public MainWindow()
{
InitializeComponent();
lstItems.ItemsSource = mOrders;
}
如果设置BindsDirectlyToSource = False,该怎么办? – Klaus78
@ Klaus78:谢谢克劳斯。请参阅下面的答案。我还发现没有其他绑定属性是必需的。所以你可以像Visibility =“{Binding IsSending,Converter = {StaticResource BooleanToVisibilityConverter1}}”。 – dotNET