1

いくつかのコンテキスト:

  • ユーザーは、複数の入力コントロール(標準のテキストボックス、コンボボックスなど)を使用してウィンドウにデータを入力します。

  • ユーザーは同じウィンドウを-readmode-で開き、以前に入力したデータを表示します。

確かに、入力フォームは簡単で、読み取りモードの場合、IsEnabled依存関係プロパティを使用して入力コントロールを無効にすることができます。

Style with Triggersを使用して、すべての入力コントロールラベルに置き換えることは可能ですか?

4

1 に答える 1

1

これにより、IsReadOnlyがtrueの場合、すべてのTextBoxがTextBlockに変わります。ご想像のとおり、スタイルにはコントロールテンプレートを変更するトリガーがあります。

<Window x:Class="SO_Xaml_ReadOnlyInputForm.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <Grid.Resources>

            <Style TargetType="TextBox">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsReadOnly}"
                                 Value="True">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="TextBox">
                                    <TextBlock Text="{TemplateBinding Text}"
                                               Width="{TemplateBinding Width}" />
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>

        </Grid.Resources>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />          
        </Grid.RowDefinitions>

        <CheckBox Grid.Row="0"
                  IsChecked="{Binding IsReadOnly}"
                  Content="Is Read-only?" />
        <StackPanel Grid.Row="1"
                    Orientation="Horizontal">
            <TextBlock>Item1</TextBlock>
            <TextBox Text="{Binding Item1Text}"
                     Width="100" />
        </StackPanel>
    </Grid>
</Window>

VIEWMODELクラス

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;

namespace SO_Xaml_ReadOnlyInputForm
{
    public class ViewModel : NotificationObject
    {
        private string _itemText;
        private bool _isReadOnly;

        public string Item1Text
        {
            get { return _itemText; }
            set
            {
                _itemText = value;
                RaisePropertyChanged(() => Item1Text);
            }
        }

        public bool IsReadOnly
        {
            get { return _isReadOnly; }
            set
            {
                _isReadOnly = value;
                RaisePropertyChanged(() => IsReadOnly);
            }
        }

        public ViewModel()
        {
            Item1Text = "This is the text";
        }
    }
}
于 2012-06-21T13:14:06.870 に答える