[英]Select one of two wpf implementations with a bool variable
In my application I want to switch between editing and just displaying data in my view.在我的应用程序中,我想在编辑和仅在视图中显示数据之间切换。 In order to edit them I have textboxes.为了编辑它们,我有文本框。 For Displaying I am using textblock.对于显示,我正在使用文本块。 Some of the Data is displayed inside a ListView.一些数据显示在 ListView 中。
In my ViewModel I have variables called "IsEditingMode" and "IsNotEditingMode" to determine the state of the view.在我的 ViewModel 中,我有名为“IsEditingMode”和“IsNotEditingMode”的变量来确定视图的 state。
Now I want something like this in wpf:现在我在 wpf 中想要这样的东西:
<switch betwen implementation>
<use textbox if IsEditingMode>
<use textblock if IsNotEditingMode>
I tried this using Converters to change the Visibility of the textbox and the textblock:我尝试使用转换器来更改文本框和文本块的可见性:
Converter:转换器:
public class BoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var boolValue = (bool)value;
if (boolValue)
return Visibility.Visible;
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Provide the Converter a Resource: <helper:BoolToVisibilityConverter x:Key="IsVisibleConverter"/>
为转换器提供资源: <helper:BoolToVisibilityConverter x:Key="IsVisibleConverter"/>
Use the converter to change the Visibility of the elements in my xaml:使用转换器更改我的 xaml 中元素的可见性:
<StackPanel>
<TextBlock Text ="{Binding TextVariableInVM}" Visibility="{Binding IsNotEditingMode, Converter={StaticResource IsVisibleConverter}}"/>
<TextBox Text ="{Binding TextVariableInVM}"
Visibility="{Binding IsEditingMode,
Converter={StaticResource IsVisibleConverter}}"/>
</StackPanel>
The result is:结果是:
For a small number of displayed variables this is just fine.对于少量显示的变量,这很好。 In my real application I have a huge amount of displayed variables and some of them are displayed using listview.在我的实际应用程序中,我有大量显示的变量,其中一些是使用列表视图显示的。 In that case my UI is getting really slow.在那种情况下,我的用户界面变得非常慢。
I believe that this is due to the fact that I bind each variable twice.我相信这是因为我将每个变量绑定了两次。
Is there a more elegant solution to do that editing <-> not editing switch?是否有更优雅的解决方案来进行编辑 <-> 不编辑切换?
Why not use only a TextBox
element for both situations?为什么不在这两种情况下都只使用一个TextBox
元素呢? TextBox
has a property called IsReadOnly
that you can set to true or false depending on your action. TextBox
有一个名为IsReadOnly
的属性,您可以根据您的操作将其设置为true或false 。
Is this code enough for your need's?这段代码是否足以满足您的需要? It's a TextBox
that binds your text, and changes from edit mode or not.它是一个绑定文本的文本TextBox
,可以从编辑模式更改或不更改。
<TextBox Text="{Binding TextVariableInVM}" IsReadOnly="{Binding IsNotEditingMode}"/>
If you complain about UI being slow, this example will be faster and more elegant because:如果你抱怨 UI 太慢,这个例子会更快更优雅,因为:
StackPanel
不需要StackPanel
Boolean
to Visibility
不需要从Boolean
转换为Visibility
You may also want to remove the TextBox Border when it is read-only by a Style like this:当 TextBox Border 被 Style 设为只读时,您可能还想删除它:
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsReadOnly" Value="True">
<Setter Property="BorderBrush" Value="{x:Null}"/>
</Trigger>
</Style.Triggers>
</Style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.