0

我目前正在开发一个 WPF 应用程序,该应用程序包含一个具有 3 列的数据网格,Closed如XAML 代码所示,它们分别链接到 3 个布尔数据对象成员(和)。单击每个切换按钮时,绑定的布尔属性必须更改,并且基于布尔数据,切换按钮的图像必须更改。单击相应的切换按钮后,后端代码中的数据正在更改,但无法正常工作。CheckedActiveIsClosedIsCheckIsActiveDataTrigger

XAML 代码:

        <DataGrid.Columns>
            ...
            <DataGridTemplateColumn Header="Closed" Width="60">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ToggleButton IsChecked="{Binding Path=IsClosed, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                      IsEnabled="{Binding Path=IsCheck, Converter={StaticResource toggleButtonEnableConverter}}">
                            <ToggleButton.Content>
                                <Image>
                                    <Image.Style>
                                        <Style TargetType="Image">
                                            <Setter Property="Source" Value="{Binding Path=IsClosed, Converter={StaticResource imageConverter}}"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding Path=IsClosed, RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}}" Value="true">
                                                    <Setter Property="Source" Value="{Binding Path=IsClosed, Converter={StaticResource imageConverter}}"/>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding Path=IsClosed, RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}}" Value="false">
                                                    <Setter Property="Source" Value="{Binding Path=IsClosed, Converter={StaticResource imageConverter}}"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Image.Style>
                                </Image>
                            </ToggleButton.Content>

                        </ToggleButton>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Checked" Width="60">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ToggleButton Visibility="{Binding Path=IsClosed, 
                                                           Converter={StaticResource ResourceKey=booleanToVisibilityConverter}}" 
                                      IsChecked="{Binding Path=IsCheck, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                      IsEnabled="{Binding Path=IsActive, Converter={StaticResource toggleButtonEnableConverter}}">
                            <ToggleButton.Content>
                                <Image>
                                    <Image.Style>
                                        <Style TargetType="Image">
                                            <Setter Property="Source" Value="{Binding Path=IsCheck, Converter={StaticResource imageConverter}}"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding Path=IsCheck, RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}}" Value="true">
                                                    <Setter Property="Source" Value="{Binding Path=IsCheck, Converter={StaticResource imageConverter}}"/>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding Path=IsCheck, RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}}" Value="false">
                                                    <Setter Property="Source" Value="{Binding Path=IsCheck, Converter={StaticResource imageConverter}}"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Image.Style>
                                </Image>
                            </ToggleButton.Content>
                        </ToggleButton>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Active" Width="60">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ToggleButton Visibility="{Binding Path=IsCheck, 
                                                            Converter={StaticResource ResourceKey=booleanToVisibilityConverter}}"
                                      IsChecked="{Binding Path=IsActive, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
                            <ToggleButton.Content>
                                <Image>
                                    <Image.Style>
                                        <Style TargetType="Image">
                                            <Setter Property="Source" Value="{Binding Path=IsActive, Converter={StaticResource imageConverter}}"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding Path=IsActive, RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}}" Value="true">
                                                    <Setter Property="Source" Value="{Binding Path=IsActive, Converter={StaticResource imageConverter}}"/>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding Path=IsActive, RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}}" Value="false">
                                                    <Setter Property="Source" Value="{Binding Path=IsActive, Converter={StaticResource imageConverter}}"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Image.Style>
                                </Image>
                            </ToggleButton.Content>
                        </ToggleButton>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

        </DataGrid.Columns>
    </DataGrid>

转换器:

public class ImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (value is bool && ((bool)value)) ? Images.TickImage : Images.CrossImage; 
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

public class Images
    {
        public static BitmapImage TickImage = new BitmapImage(new Uri("K:\\projects\\ContentSets\\Ownership\\SOMA\\Staging\\Utility\\Images\\icon_tick.gif", UriKind.Absolute));
        public static BitmapImage CrossImage = new BitmapImage(new Uri("K:\\projects\\ContentSets\\Ownership\\SOMA\\Staging\\Utility\\Images\\icon_cross.gif", UriKind.Absolute));

    }

public class ToggleButtonEnableConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return !(bool)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

图像在启动时根据布尔值加载(因此图像源没有问题),但图像在单击 ToggleButton 时会发生变化(尽管绑定的属性会受到影响)。

4

1 回答 1

1

Update your DataTrigger binding to properties of Togglebutton like IsChecked, IsEnabled etc instead of context properties:

  <DataTrigger Binding="{Binding Path=IsChecked, RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}}" Value="true">
    <Setter Property="Source" Value="{Binding Path=IsClosed, Converter={StaticResource imageConverter}}"/>
  </DataTrigger>
  <DataTrigger Binding="{Binding Path=IsChecked, RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}}" Value="false">
      <Setter Property="Source" Value="{Binding Path=IsClosed, Converter={StaticResource imageConverter}}"/>
   </DataTrigger>

Also I dont understand why do you need triggers.. you have not changed anything in trigger... The binding you did in the trigger setter is same as default setter... so it should work without even triggers

于 2013-10-04T13:51:18.130 回答