0

I have a project, which contain converter. When I click in DGKraj (one of the many ComboBox) and during initialization converter is automatically called. How can I call the converter only during initialization?

public class CountrySingleConverter : IValueConverter
{
    Funkcje FK = new Funkcje();
    List<string> kraje = new List<string>();

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {    
        if (value != null && !string.IsNullOrWhiteSpace(value.ToString()))
        { 
            kraje = FK.kraj_wybierz(value.ToString(), Edycja.dgKraj);
                return kraje;
        }
        return null;
    }

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

XAML

                        <DataGridComboBoxColumn x:Name="DGKraj" Header="Kraj" Width="120" CanUserSort="False" SelectedValueBinding="{Binding Kraj}" >
                            <DataGridComboBoxColumn.ElementStyle>
                                <Style TargetType="ComboBox">
                                    <Setter Property="ItemsSource" Value="{Binding Path=Kontynent,Converter={StaticResource CountryConverter}}"/>
                                    <Setter Property="SelectedValue" Value="{Binding Path=Kraj}"/>
                                </Style>
                            </DataGridComboBoxColumn.ElementStyle>
                            <DataGridComboBoxColumn.EditingElementStyle>
                                <Style TargetType="ComboBox">
                                    <Setter Property="ItemsSource" Value="{Binding Path=Kontynent,Converter={StaticResource CountryConverter}}" />
                                    <Setter Property="SelectedValue" Value="{Binding Path=Kraj}"/>

                                </Style>
                            </DataGridComboBoxColumn.EditingElementStyle>
                        </DataGridComboBoxColumn>
  • During initialization of what? This is a bit vague – lboshuizen Mar 17 '14 at 14:15
  • Initialization of project. – user3388075 Mar 17 '14 at 14:18
  • 1
    What you want to do? This doesn't make any sense. Post your actual problem. There might be some better way to do that. – Rohit Vats Mar 17 '14 at 14:21
  • It's not clear what you want but perhaps using Mode=OneTime on your "binding" will do it...thus – Colin Smith Mar 17 '14 at 16:13
  • No it's not working, but it is good thinking. – user3388075 Mar 17 '14 at 16:24
  • That's not how converters work. When the "get" function of a binding is called, the "Convert" function is always called, and when the "set" function is called, the "ConvertBack" function is invoked. You could put some logic in the converter to detect if it is the "initialization" conversion and do it differently otherwise (definitely NOT recommended), or just make sure your bindings only get when you want them to. "Mode=OneTime" should have been the right answer, if that isn't working you will need to post more details. – BradleyDotNET Mar 17 '14 at 17:32

0 Answers0