【问题标题】:The property Content can only be set once属性 Content 只能设置一次
【发布时间】:2017-09-04 10:44:14
【问题描述】:

我从 wpf 开始,我想使用静态资源,当我添加标签时出现错误:属性 Content 只能设置一次,现在当我在 stackoverflow 上搜索时,我了解到窗口标签可以只有一个子元素,在我正在阅读的书上,静态资源标签就在网格标签上方,这是如何工作的?这是我的代码:

<Window x:Class="BaseBallSimulator.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibilit/2006"
        xmlns:local="clr-namespace:BaseBallSimulator"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" >
    <StaticResource></StaticResource>
    <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="245*"/>
        <ColumnDefinition Width="272*"/>
    </Grid.ColumnDefinitions>      
        <StackPanel>
        <StackPanel Margin="10">
            <TextBlock Text="Trajectory"></TextBlock>
            <TextBox Name="TrajectoryTextBox" Text="0" Margin="0,5,0,0" Height="20"></TextBox>
        </StackPanel>
        <StackPanel Margin="10">
            <TextBlock Text="Distance"></TextBlock>
            <TextBox Name="DistanceTextBox" Text="0" Margin="0,5,0,0" Height="20"></TextBox>
        </StackPanel>
        <Button Name="PlayButton" Content="PRESS To PLAY" Margin="10" Click="PlayButton_Click"  ></Button>
    </StackPanel>
   <StackPanel Grid.Column="1">
        <StackPanel Margin="5">
            <TextBlock Text="Pitcher Says" Margin="5"/>
            <ListView x:Name="PitcherSaysListView" Margin="5" Height="110" />
        </StackPanel>
        <StackPanel Margin="5,0,5,5">
            <TextBlock Text="Fan Says" Margin="5"/>
            <ListView x:Name="FanSaysListView" Margin="5" Height="110" />
        </StackPanel>
    </StackPanel>
</Grid>
</Window>

【问题讨论】:

  • 你为什么要添加?删除它,它就可以工作了......
  • 是的,我知道它可以工作,但我需要一个该标签来设置静态资源,否则怎么办?

标签: c# wpf


【解决方案1】:

删除&lt;StaticResource&gt;&lt;/StaticResource&gt;,您的代码将编译。

任何资源都应该添加到&lt;Window.Resources&gt;

<Window ...>
    <Window.Resources>
        <SolidColorBrush x:Key="brush" Color="Red" />
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="245*"/>
            <ColumnDefinition Width="272*"/>
        </Grid.ColumnDefinitions>
        <StackPanel>
            <StackPanel Margin="10">
                <TextBlock Text="Trajectory"></TextBlock>
                <TextBox Name="TrajectoryTextBox" Text="0" Margin="0,5,0,0" Height="20"></TextBox>
            </StackPanel>
            <StackPanel Margin="10">
                <TextBlock Text="Distance"></TextBlock>
                <TextBox Name="DistanceTextBox" Text="0" Margin="0,5,0,0" Height="20"></TextBox>
            </StackPanel>
            <Button Name="PlayButton" Content="PRESS To PLAY" Margin="10"  ></Button>
        </StackPanel>
        <StackPanel Grid.Column="1">
            <StackPanel Margin="5">
                <TextBlock Text="Pitcher Says" Margin="5"/>
                <ListView x:Name="PitcherSaysListView" Margin="5" Height="110" />
            </StackPanel>
            <StackPanel Margin="5,0,5,5">
                <TextBlock Text="Fan Says" Margin="5"/>
                <ListView x:Name="FanSaysListView" Margin="5" Height="110" />
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

虽然定义一个空的&lt;StaticResource&gt; 元素没有任何意义。

【讨论】:

  • 我需要它来制作静态资源
  • “它?”什么应该是静态资源?
  • 为什么这被否决了! mm8 关于“删除 并且您的代码将编译。” 是正确的
  • 尽了我的一份力 :) 因为它在删除后也为我编译了
  • 哦,我看错了,Windows.Resource 而不是静态资源。我知道我有多愚蠢,谢谢解决它的人
【解决方案2】:

静态(以及动态)资源通过所谓的资源字典进行管理。您可以添加显式资源字典文件并在某处引用它。

此外,XAML 中的每个 UIElement 都可以拥有自己的一组资源(静态和动态)。静态资源和动态资源的定义方式完全相同,区别在于分配方式。

例如,您可以为整个窗口创建一个颜色资源

<Window [...]>
    <Window.Resources>
        <SolidColorBrush x:Key="myColor">Blue</Color>
    </Window.Resources>
</Window>

在资源标签内定义的资源仅对元素本身和任何子元素可见。 x:Key-Attribute 为资源提供了一个名称,您可以使用它来引用它。然后,您可以使用

<Grid Background="{StaticResource myColor}"/>

此外,您还可以在这里定义资源是以静态方式使用还是以动态方式使用(使用 DynamicResource 代替)。然后您可以使用之前设置的密钥来访问您的资源。

如果您想更改特定控件的外观,例如它的字体大小、边距和颜色,您可以使用所谓的样式,而不是以乏味的方式分配每个属性。样式的定义方式与普通资源完全相同

<Window.Resources>
    <Style x:Key="MyTextBlock" TargetType="TextBlock">
        <Setter Property="FontSize" Value="28"/>
        <Setter Property="Background" Value="Red"/>
        <Setter Property="Margin" Value="5"/>
    </Style>
</Window.Resources>

注意两点:除了x:Key-Attribute,我们还指定了TargetType-Attribute 来告诉VisualStudio 我们希望更改哪个元素。其次,我们使用 Setter 来更改该元素的特定属性。要将样式应用于控件,只需使用

<TextBlock Style="{StaticResource MyTextBlock}">My styled Textblock</TextBlock>

奖励:使用样式时,您可以省略x:Key-属性。这将自动将该样式应用于TargetType 类型的所有子控件。

【讨论】:

  • 感谢 Man 的帮助
猜你喜欢
  • 2016-12-04
  • 1970-01-01
  • 1970-01-01
  • 2017-02-02
  • 2012-05-10
  • 2011-03-08
  • 1970-01-01
  • 2010-10-24
相关资源
最近更新 更多