2

私はこのコードを持っています:

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();
        }
    }
}

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}" />

SearchFieldSearchStrategy Strategyには、 からアクセスする必要があるいくつかのメソッドとフィールドがありますTextToTipConverter。どうすればそれに到達できますか?

4

3 に答える 3

2

ヒント テキスト ボックスを検索テキスト ボックスに直接バインドする代わりに、検索テキスト ボックスと SearchFieldViewModel のプロパティとの間に双方向のバインドを作成してみてください。これにより、検索テキスト ボックスへの変更が SearchFieldViewModel に自動的にプッシュ ダウンされます。

検索文字列が SearchFieldViewModel に入ったら、それを SearchStrategy と一緒に TipViewModel にバンドルし、TipViewModel をヒント テキスト ボックスの DataContext として使用できます。以下のコードを参照してください。

SearchField.xaml

<UserControl x:Class="SilverlightApplication.SearchField"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:SilverlightApplication"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <UserControl.Resources>
        <local:TextToTipConverter x:Key="TextToTip" />
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">
        <TextBox x:Name="Search" Grid.Column="0" Canvas.ZIndex="1" Text="{Binding Path=SearchString, Mode=TwoWay}"
                 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 Path=TipViewModel, Converter={StaticResource TextToTip}}"/>
    </Grid>
</UserControl>

SearchField.xaml.cs

public partial class SearchField : UserControl
{
   public SearchField()
   {
       InitializeComponent();

       this.Loaded += (s, e) => this.LayoutRoot.DataContext = new SearchFieldViewModel();
    }
}

SearchFieldViewModel.cs

public class SearchFieldViewModel
{
    public string SearchString { get; set; }
    public SearchStrategy SearchStrategy { get; set; }

    public TipViewModel TipViewModel
    {
        get
        {
            return new TipViewModel
            {
                SearchString = this.SearchString,
                SearchStrategy = this.SearchStrategy
            };
        }
    }
}

TipViewModel.cs

public class TipViewModel
{
    public string SearchString { get; set; }
    public SearchStrategy SearchStrategy { get; set; }
}

TextToTipConverter.cs

public class TextToTipConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        TipViewModel tipViewModel = value as TipViewModel;
        SearchStrategy strategy = tipViewModel.SearchStrategy;
        string searchString = tipViewModel.SearchString;

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

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
于 2012-12-21T19:23:39.017 に答える
2

SearchField.xaml はビューで、SearchField.xaml.cs はコード ビハインドです。msdn で MVVM、データバインディング、ViewModel に関する情報を読むことができます。データをバインドする ViewModel というクラスを作成できます。たとえば、次のクラスを想像してください。

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

SearchField.xaml.cs は次のようになります。

public partial class SearchField : UserControl
{
    private SearchViewModel viewModel;

    public SearchField() 
    {
        this.viewModel = new SearchViewModel ();
        this.DataContext = this.viewModel;
    }
}

今あなたのxamlで、

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

viewModel のデータを TextBox にバインドします。次のことが可能になります。

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

コンバーターでは、value という名前のパラメーターが、プロパティを取得できるビュー モデルになります。

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

私がはっきりしているかどうかはわかりません。

于 2012-12-21T19:16:47.620 に答える
1

SearchStrategy を ViewModel に配置し、Binding を使用して渡します。あなたの質問に答えられるかどうかわかりませんが、もっと情報を提供してください

于 2012-12-21T18:49:14.420 に答える