某些背景信息:
用户使用多个输入控件(标准文本框,组合框等)将数据输入到窗口中。
用户在-readmode中打开相同的窗口 - 显示以前输入的数据。
当然,输入表单很简单,对于readmode,我可以使用IsEnabled dependancyproperty来禁用输入控件。
是否可以使用带触发器的样式将所有输入控件替换为标签?
答案 0 :(得分:1)
当IsReadOnly为true时,这会将所有TextBox转换为TextBlocks。正如您猜测的那样,Style上有一个触发器可以更改控件模板。
<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>
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";
}
}
}