Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从设置中将textblock内容设置为字符串_C#_Wpf_Xaml - Fatal编程技术网

C# 从设置中将textblock内容设置为字符串

C# 从设置中将textblock内容设置为字符串,c#,wpf,xaml,C#,Wpf,Xaml,我的表单中有2个文本块。 大概是这样的: <TextBlock Name="applicationname" Text="{binding applicationname?}"/> <TextBlock Name="applicationname" Text="{binding settings.stringVersionNumber}"/> 我想将第一个文本块内容设置为自动显示应用程序名称,另一个设置为显示应用程序设置中保存的字符串 我应该使用wpf代码更改值,还是直

我的表单中有2个文本块。 大概是这样的:

<TextBlock Name="applicationname" Text="{binding applicationname?}"/>
<TextBlock Name="applicationname" Text="{binding settings.stringVersionNumber}"/>
我想将第一个文本块内容设置为自动显示应用程序名称,另一个设置为显示应用程序设置中保存的字符串


我应该使用wpf代码更改值,还是直接在xaml中绑定文本块?

您可以直接在xaml中数据绑定到静态属性,包括应用程序设置:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:l="clr-namespace:WpfApplication1"
        xmlns:p="clr-namespace:WpfApplication1.Properties"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding Source={x:Static l:MainWindow.AssemblyTitle}}"/>
            <TextBlock Text="{Binding Source={x:Static p:Settings.Default}, Path=VersionNumber}"/>
        </StackPanel>
    </Grid>
</Window>

请注意,您不能为两个元素指定相同的名称。

太好了,非常感谢……关于如何显示未保存在设置中的应用程序名称,您有什么想法吗?您的意思是[assembly:AssemblyTitle]属性中给出的值?
public static string AssemblyTitle
{
    get 
    {
        return Assembly.GetExecutingAssembly()
                       .GetCustomAttributes(typeof(AssemblyTitleAttribute), false)
                       .Cast<AssemblyTitleAttribute>()
                       .Select(a => a.Title)
                       .FirstOrDefault();
    }
}