2

エディター(テキストボックス)があり、クリックされたボタンに基づいて異なるテキストをバインドしたいと思います。

ボタンのコマンドを使用して、編集したい文字列をcommandparameterを介して渡し、テキストボックスにバインドされている文字列を更新することができます。これは機能しますが、テキスト(コマンドパラメータを介して渡された)とテキストボックスのテキストの間にバインディングがないため、変更は保存されません。

私の質問は、ビューモデルから直接テキストボックスにアクセスせずに、このバインドを適切に実装するにはどうすればよいですか?

編集:それはおそらく私が達成しようとしていることは曖昧です。私はそれを少し明確にしようとします:

したがって、いくつかの異なるボタンがあるとしましょう。そのうちの1つをクリックすると、文字列がエディターのテキストボックスにバインドされ、後で変更して保存できます。

     <Button Content="Edit query" Command="{Binding ShowQueryInEditorCommand}" CommandParameter="{Binding SomeSqlStringToBeEdited}"/>

     <Button Content="Edit query" Command="{Binding ShowQueryInEditorCommand}" CommandParameter="{Binding SomeOtherSqlStringToBeEdited}"/>

これは、コマンドが実行するものです。

    public void ShowQueryInEditor(object o)
    {            
        string SqlStatementParam = o as string;
        if (SqlStatementParam != null)
            SQLStatement = SqlStatementParam;            
    }

そして、エディターのTextBox自体は次のようになります。

    <TextBox Text="{Binding SQLStatement}">

ご覧のとおり、これはSQLStatement文字列を設定するだけなので非常に初歩的なものですが、それらの間にバインドがないため、変更をSomeSqlStringToBeEdited/SomeOtherSqlStringToBeEditedに反映することはできません。これは、ボタンがクリックされたときにその文字列をテキストボックスにバインドするために私が達成したいことです。

4

2 に答える 2

2

考えられる基本的な方法は 2 つあります。コードを使用する方法と、Xaml を使用する方法です。

コードでは、ViewModel からテキスト ボックスにアクセスする代わりに、"DisplayText" または "SelectedText" の ViewModel に新しいプロパティを追加するか、シナリオで意味のあるものを追加します。代わりにテキスト ボックスをそのプロパティにバインドし、必要な残りのロジックをセッター (または、コールバックの場合) 内DependencyPropertyOnPropertyChanged配置します。これにより、すべてのロジックが ViewModel に保持され、Xaml が気にする必要がなくなります。

または、Xaml では、トリガーとテンプレートを使用して、選択したボタンに応じてテキスト ボックスを変更できます。おそらくあなたの説明から、複数のテキストボックスを各文字列にバインドし、クリックされたボタンに基づいて表示されるテキストボックスを切り替えることをお勧めします。これにより、ViewModel はこのディスプレイ固有のロジックを認識しなくなり、後で簡単に変更できるようになります。

個人的には、Xaml アプローチをお勧めしますが、それは特定の状況によって異なります。

于 2012-08-10T08:17:39.773 に答える
0

によると

しかし問題は、ボタンが動的に作成されることです

1)クエリテキストとボタンを次のようにビューモデルにラップします。

public class ViewModel : ViewModelBase
{
    public ViewModel()
    {
        this.turnIsSelectedOnCommand = new RelayCommand(() => IsSelected = true);
    }

    public String Text
    {
        get { return text; }
        set
        {
            if (text != value)
            {
                text = value;
                OnPropertyChanged("Text");
            }
        }
    }
    private String text;

    public Boolean IsSelected
    {
        get { return isSelected; }
        set
        {
            if (isSelected != value)
            {
                isSelected = value;
                OnPropertyChanged("IsSelected");
            }
        }
    }
    private Boolean isSelected;

    public RelayCommand TurnIsSelectedOnCommand
    {
        get { return turnIsSelectedOnCommand; }
    }
    private readonly RelayCommand turnIsSelectedOnCommand;
}

2)動的に作成されたテキスト/ボタンをコレクションに入れます。簡単にするために、それらを配列に追加しました。

    public MainWindow()
    {
        InitializeComponent();
        DataContext = new[] 
        { 
            new ViewModel { Text = "SELECT * FROM Foo", IsSelected = true },
            new ViewModel { Text = "SELECT * FROM Bar" },
            new ViewModel { Text = "DROP TABLE Foo" },
            new ViewModel { Text = "DROP TABLE Bar" },
        };
    }

3)コレクションをListBoxでバインドし、エディターをText選択したアイテムのでバインドします。

<ListBox Grid.Row="0" Margin="5" ItemsSource="{Binding}" x:Name="lbItems">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Right"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                                 Color="Transparent"/>
            </Style.Resources>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Button Content="Edit query" Command="{Binding TurnIsSelectedOnCommand}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

<TextBox Grid.Row="1" Margin="5" Text="{Binding Path=SelectedItem.Text, ElementName=lbItems}" />

いくつかのスタイル変更を追加しました。まず、ボタンのレイアウトを変更します。2つ目は、ボタンを押してViewModelが選択されると、リストボックス項目も選択されることを意味します。3番目は、選択したアイテムの背景から選択を非表示にします。

お役に立てれば。

于 2012-08-10T09:43:06.927 に答える