Search code examples
c#referencecode-behindwindows-phone-8

Get reference to code-behind class instance from IValueConverter


I have this code:

namespace Test
{
    public partial class SearchField : UserControl
    {
        public SearchStrategy Strategy { get; set; }
        public SearchField() { InitializeComponent(); }
    }

    public class TextToTipConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            SearchStrategy Strategy = // How do I get reference to SearchField.Strategy from here?

            return Strategy.parseInput<string> (value.ToString(), (first, inp) => Strategy.tipMap.ContainsKey(first) && inp.Length == 1 ? first + Strategy.tipMap[first] : "", AppResources.GeneralSearchTip);
        }

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

Code in XAML:

<UserControl.Resources>
        <controls:TextToTipConverter x:Key="TextToTip" />
</UserControl.Resources>
...
<TextBox x:Name="Search" Grid.Column="0" Canvas.ZIndex="1" 
                 Style="{StaticResource SearchBoxStyle}" Foreground="{StaticResource PhoneForegroundBrush}" />

<TextBox x:Name="Tip" Grid.Column="0" Canvas.ZIndex="0" IsReadOnly="True"
                 Style="{StaticResource SearchBoxStyle}" Opacity="0.5" Foreground="{StaticResource PhoneAccentBrush}"
                 Text="{Binding ElementName=Search, Converter={StaticResource TextToTip}, Path=Text}" />

SearchField's SearchStrategy Strategy has some methods and fields that I need to access from TextToTipConverter. How can I get to it?


Solution

  • SearchField.xaml is the view and SearchField.xaml.cs is the code behind. You can read information about MVVM, databinding and ViewModel on msdn. You can make a class called ViewModel on which you will bind your data. For instance, imagine the following class :

    public class SearchViewModel : INotifyPropertyChanged
    {
        public string Text { get; set; }
        public SearchStrategy Strategy { get; set; }
    }
    

    Your SearchField.xaml.cs will be :

    public partial class SearchField : UserControl
    {
        private SearchViewModel viewModel;
    
        public SearchField() 
        {
            this.viewModel = new SearchViewModel ();
            this.DataContext = this.viewModel;
        }
    }
    

    Now in your xaml,

    TextBox x:Name="Search" Text="{Binding Text}"
    

    will bind the data in the viewModel with the TextBox And you will be able to do :

    TextBox x:Name="Tip" Text="{Binding, Converter={StaticResource TextToTip}, Path=Text}"
    

    In the converter, the parameter named value will be your view model on which you can get properties :

    SearchViewModel vm = (SearchViewModel) value;
    vm.Strategy;
    vm.Text
    

    I don't know if i'm clear.