[英]How to make this code more efficient?
As i am not very advanced in C# yet, I try to learn how to make my code more efficient. 由于我在C#方面不是很先进,我尝试学习如何使我的代码更有效率。 I stored a lot of strings in some of the properties.
我在一些属性中存储了很多字符串。
At the start of the application, i load all the seperatie properties into the textboxes. 在应用程序的开始,我将所有seperatie属性加载到文本框中。 I now ise this code to load them all:
我现在是这个代码加载它们:
private void LoadStoredStrings()
{
txtT1S1.Text = Properties.Settings.Default.strT1L1;
txtT1S2.Text = Properties.Settings.Default.strT1L2;
txtT1S3.Text = Properties.Settings.Default.strT1L3;
txtT1S4.Text = Properties.Settings.Default.strT1L4;
txtT1S5.Text = Properties.Settings.Default.strT1L5;
txtT1S6.Text = Properties.Settings.Default.strT1L6;
txtT1S7.Text = Properties.Settings.Default.strT1L7;
txtT1S8.Text = Properties.Settings.Default.strT1L8;
txtT1S9.Text = Properties.Settings.Default.strT1L9;
txtT1S10.Text = Properties.Settings.Default.strT1L10;
}
Obvious i can see the logic that each stored propertie ending with T1L1
also fits to the txt that ends with T1S1
. 显而易见,我可以看到每个以
T1L1
结尾的存储属性也适合以T1L1
结尾的txt的T1S1
。 I just know this should be done in a more elegant and solid way than what i did now. 我只知道这应该以比我现在更优雅和坚实的方式完成。 Could anyone push me in the right direction?
有人能把我推向正确的方向吗?
you can bind your properties directly to your textboxes 您可以将属性直接绑定到文本框
<UserControl xmlns:Properties="clr-namespace:MyProjectNamespace.Properties" >
<TextBox Text="{Binding Source={x:Static Properties:Settings.Default}, Path=strT1L1, Mode=TwoWay}" />
If you can get all of those constants into a List<string>
, you could use it to bind to an ItemsControl
with TextBlock
inside: 如果你可以将所有这些常量都放到
List<string>
,你可以用它来绑定到带有TextBlock
的ItemsControl
:
Code behind or View Model 代码隐藏或查看模型
private ObservableCollection<string> _defaultProperties = new ObservableCollection<string>();
public ObservableCollection<string> DefaultProperties
{
get { return _defaultProperties; }
}
XAML XAML
<ListBox ItemsSource="{Binding Path=DefaultProperties"}>
<ListBox.ItemTemplate>
<DataTemplate>
<!--Just saying "Binding" allows binding directly to the current data context vs. a property on the data context-->
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.