2

私はWPFとMVVMが初めてで、正しい方向へのポインタを探しています。

Microsoft Word の「n ページ、現在のページまたは選択範囲の印刷」に似たものを実装したいと思います。

Microsoft Word 印刷 n ページ

私の例では、 radio1またはradio2を切り替えるラジオボタンがありますが、

  1. ビューモデルの TimeType が何であれデフォルト
    1. TimeType == Type1=> radio1 が選択され、text1 = ""
    2. TimeType == Type2=> radio2 が選択され、text1 = ViewModel.Time
  2. ユーザーがradio1を選択した場合、 text1をクリアしたい。
  3. text1にを入力すると、ラジオ ボタンが radio2 に切り替わりそれに応じてビュー モデルが更新されます。

コンバーターのさまざまな例を見て試してみましたが、動作にさまざまな影響を与えてうまく連携させる方法がわかりません。

ロジックを実行するためにビューモデルに何かを実装する必要があると感じていますが、何にバインドすればよいかわかりません。

XAML

<Grid.Resources>
    <local:EnumToBooleanConverter x:Key="e2b" />
</Grid.Resources>
<RadioButton Name="radio1" GroupName="g1" Content="Radio 1"
    IsChecked="{Binding Path=TimeType, Converter={StaticResource e2b}, ConverterParameter={x:Static vm:TimeType.Type1}}"
    />
<RadioButton Name="radio2" GroupName="g1">
    <TextBox Name="text1"
        Text="{Binding Path=ExplicitTime, Mode=TwoWay}">
    </TextBox>
</RadioButton>

モデルを見る

// UPDATE: added INotifyPropertyChanged as per my actual code
class MagicTimeViewModel :INotifyPropertyChanged {
    public enum TimeType{Type1, Type2}
    TimeType _type; int _time;
    public TimeType TimeType{
        get{_return _type;}
        set {_type = value; Notify("TimeType");}
    }
    public int Time {
        get{_return _time;}
        set {_time = value; Notify("Time");}
    }
    void Notify(string name)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}
4

2 に答える 2

0

通常、このようなことを行うには、ListBox(選択したアイテムを追跡したいので、一度に1つのアイテムしか選択できないため)を使用し、スタイルを上書きして次のように表示しますRadioButtons

<Style x:Key="RadioButtonListBoxStyle" TargetType="{x:Type ListBox}">
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle" />
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ListBoxItem}" >
                <Setter Property="Margin" Value="2, 2, 2, 0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border Background="Transparent">
                                <RadioButton IsHitTestVisible="False" Focusable="false" 
                                    Content="{TemplateBinding ContentPresenter.Content}"  
                                    IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

はプロパティとプロパティをViewModel格納し、1 つのプロパティが変更されると、関連するプロパティも更新されます。SelectedIndexTimeText

たとえば、SelectedIndexが 0 になったら、 をクリアしTimeTextます。TimeText変更時はSelectedIndex1に設定

XAML の例を次に示します。

<ListBox x:Name="MyListBox"
         Style="{StaticResource RadioButtonListBoxStyle}"
         SelectedIndex="{Binding SelectedIndex}">

    <ListBoxItem>All</ListBoxItem>
    <ListBoxItem>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Pages" />
            <TextBox Text="{Binding DataContext.TimeText, ElementName=MyListBox, UpdateSourceTrigger=PropertyChanged}" />
        </StackPanel>
    </ListBoxItem>
</ListBox>
于 2012-06-15T13:07:15.067 に答える
0

あなたはあなたに実装する必要がありINotifyPropertyChangedますViewModel

于 2012-06-15T11:59:31.560 に答える