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?
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 :
Your SearchField.xaml.cs will be :
Now in your xaml,
will bind the data in the viewModel with the TextBox
And you will be able to do :
In the converter, the parameter named value will be your view model on which you can get properties :
I don’t know if i’m clear.