加载时调用 IValueConverter 的 ConvertBack

发布于 2024-11-16 12:30:08 字数 2610 浏览 8 评论 0原文

我将组合框(即列表框项模板的一部分)绑定到枚举,所选项目绑定到绑定到列表框的集合。
我使用转换器来实现一些逻辑。

问题是 ConvertBack 不会在启动时调用,而是仅在我重新选择组合框中的项目时调用。

我也需要它在启动时调用。

public enum FullEnum 
    {
       Apple,
       Banana,
       Pear
    }
<Window.Resources>
   <local:EnumConverter x:Key="enumConverter"/>

   <ObjectDataProvider x:Key="DataT"
                       MethodName="GetValues" 
                       ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:FullEnum" />
            </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>
<Grid>
   <Grid.RowDefinitions>
        <RowDefinition Height="190*" />
       <RowDefinition Height="71*" />
   </Grid.RowDefinitions>
   <ListBox Name="list1" Margin="0,0,0,37">
        <ListBox.ItemTemplate>
           <DataTemplate>
             <StackPanel>
               <TextBlock Text="{Binding Path=Label}"></TextBlock>
               <ComboBox Height="23" Width="90"                                 
                         ItemsSource="{Binding Source={StaticResource DataT}}"                                                                  
                         SelectedValue="{Binding Path=Oped, Converter={StaticResource enumConverter}}">
                </ComboBox>
             </StackPanel>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>
  </Grid>
List<Item1> list = new List<Item1>();
public Window1()
{
     InitializeComponent();
     list.Add(new Item1 { Label="label1" });
     list.Add(new Item1 { Label = "label2" });
     list.Add(new Item1 {  Label = "label3" });

     list1.ItemsSource = list;

}

    public class Item1
    {
            public FullEnum Oped { get; set; }
            public string Label { get; set; }
    }

 public class EnumConverterr : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //some code        
        }

      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((int)value != 0)
            return (EnumSuperior)value;
        return (EnumSuperior)7;
    }

    }

I bind combobox (that is a part if listbox item template) to enum, the selected item is bound to the collection that is bound to listbox.
I use a converter for some logic.

The problem is that the ConvertBack is not invoked on startup, but only when I re-select the item in combobox.

I need it to invoke also on start.

public enum FullEnum 
    {
       Apple,
       Banana,
       Pear
    }
<Window.Resources>
   <local:EnumConverter x:Key="enumConverter"/>

   <ObjectDataProvider x:Key="DataT"
                       MethodName="GetValues" 
                       ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:FullEnum" />
            </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>
<Grid>
   <Grid.RowDefinitions>
        <RowDefinition Height="190*" />
       <RowDefinition Height="71*" />
   </Grid.RowDefinitions>
   <ListBox Name="list1" Margin="0,0,0,37">
        <ListBox.ItemTemplate>
           <DataTemplate>
             <StackPanel>
               <TextBlock Text="{Binding Path=Label}"></TextBlock>
               <ComboBox Height="23" Width="90"                                 
                         ItemsSource="{Binding Source={StaticResource DataT}}"                                                                  
                         SelectedValue="{Binding Path=Oped, Converter={StaticResource enumConverter}}">
                </ComboBox>
             </StackPanel>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>
  </Grid>
List<Item1> list = new List<Item1>();
public Window1()
{
     InitializeComponent();
     list.Add(new Item1 { Label="label1" });
     list.Add(new Item1 { Label = "label2" });
     list.Add(new Item1 {  Label = "label3" });

     list1.ItemsSource = list;

}

    public class Item1
    {
            public FullEnum Oped { get; set; }
            public string Label { get; set; }
    }

 public class EnumConverterr : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //some code        
        }

      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((int)value != 0)
            return (EnumSuperior)value;
        return (EnumSuperior)7;
    }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

做个ˇ局外人 2024-11-23 12:30:08

WPF 在初始化时不会调用返回转换器,因为它刚刚从数据上下文中获取了初始值。数据绑定的源和目标应具有相同的值,因此没有理由更新源。

您尚未发布转换回逻辑,但转换器中必须有一些“有状态”逻辑。转换器应该是无状态的(无副作用,不可变)。所有转换都应基于转换期间未修改的值、参数和转换器属性。

如果您的转换器是无状态的,您所需要做的就是正确初始化数据源,并且您应该不再需要初始转换回调。

The return converter is not called by WPF on initialization because it has just gotten the initial values from the data context. The source and target of the data binding should have the same values so there is no reason to update the source.

You have not posted your convert back logic, but you must have some "state-ful" logic in the converter. Converters should be stateless (no side-effects, immutable). All of the conversion should be based on the value, a parameter, and converter properties that are not modified during the conversion.

If your converter is stateless, all you need to do is initialize the data source properly and you should no longer need that initial convert back call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文