我有一个 可观察集合 绑定(bind)到 列表框 bool 值 绑定(bind)到 的属性按钮 .然后我定义了两个转换器 ,一个对集合进行操作,另一个对 bool 属性进行操作。每当我修改 bool 属性时,都会调用转换器的 Convert 方法,而如果我修改 observable 集合,则不会调用相同的方法。我错过了什么??

片段供您引用,

xaml 片段,

<Window.Resources> 
    <local:WrapPanelWidthConverter x:Key="WrapPanelWidthConverter" /> 
    <local:StateToColorConverter x:Key="StateToColorConverter" /> 
</Window.Resources> 
<StackPanel> 
    <ListBox x:Name="NamesListBox" ItemsSource="{Binding Path=Names}"> 
        <ListBox.ItemsPanel> 
            <ItemsPanelTemplate> 
                <WrapPanel x:Name="ItemWrapPanel" Width="500" Background="Gray"> 
                    <WrapPanel.RenderTransform> 
                        <TranslateTransform x:Name="WrapPanelTranslatation" X="0" /> 
                    </WrapPanel.RenderTransform> 
                    <WrapPanel.Triggers> 
                        <EventTrigger RoutedEvent="WrapPanel.Loaded"> 
                            <BeginStoryboard> 
                                <Storyboard> 
                                    <DoubleAnimation Storyboard.TargetName="WrapPanelTranslatation" Storyboard.TargetProperty="X" To="{Binding Path=Names,Converter={StaticResource WrapPanelWidthConverter}}" From="525"  Duration="0:0:2" RepeatBehavior="100" /> 
                                </Storyboard> 
                            </BeginStoryboard> 
                        </EventTrigger> 
                    </WrapPanel.Triggers> 
                </WrapPanel> 
            </ItemsPanelTemplate> 
        </ListBox.ItemsPanel> 
        <ListBox.ItemTemplate> 
            <DataTemplate> 
                <Grid> 
                    <Label Content="{Binding}" Width="50" Background="LightGray" /> 
                </Grid> 
            </DataTemplate> 
        </ListBox.ItemTemplate> 
    </ListBox> 
    <Button Content="{Binding Path=State}" Background="{Binding Path=State, Converter={StaticResource StateToColorConverter}}" Width="100" Height="100" Click="Button_Click" /> 
</StackPanel>    

代码片段背后的代码
public class WrapPanelWidthConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
        ObservableCollection<string> aNames = value as ObservableCollection<string>; 
        return -(aNames.Count * 50); 
    } 
 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
        throw new NotImplementedException(); 
    } 
} 
 
 
public class StateToColorConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
        bool aState = (bool)value; 
        if (aState) 
            return Brushes.Green; 
        else 
            return Brushes.Red; 
    } 
 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
        throw new NotImplementedException(); 
    } 
}    

请您参考如下方法:

我认为 Binding 中的转换器如果 Binding 总是调用source 已更新并通知该更新(作为 DependencyProperty 或使用 INotifyPropertyChanged )。但是,ObservableCollection不引发 PropertyChanged如果已添加或删除项目,则事件,但它会引发 CollectionChanged 事件。如果集合中的项目发生更改,它根本不会引发任何事件。即使项目本身提高 PropertyChanged ,这不会更新 BindingBinding 开始收藏source 不是项目,而是集合。

我担心你的方法不会这样工作。您可以直接绑定(bind)到 ObservableCollection.Count并向其添加适当的数学转换器以执行求逆和乘法,但 Count属性不执行更改通知,因此没有选项。我认为您必须在 ViewModel 或代码隐藏中提供另一个属性来处理这些情况......


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!