开发者

Invoke IValueConverter's ConvertBack on load

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>
  </Gr开发者_JAVA百科id>
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;
    }

    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜