我想让一个StackPanel里面的所有控件(不管是什么类型)都有一样的宽度和高度,我写成
<StackPanel.Resources>
<Style TargetType="{x:Type FrameworkElement}">
<Setter Property="Width" Value="60" />
<Setter Property="Height" Value="50" />
</Style>
</StackPanel.Resources>
却没有效果。如果指定FrameworkElement为某个具体类,如Rectangle、Ellipse等则可以。但我不知道加进来的具体是什么类型的控件。请问如何解决?

解决方案 »

  1.   

    没玩过Style,那个是美工的事。不过1楼肯定是连WPF都没玩过,居然说出个全局变量
      

  2.   

    刚才google搜索了下Style,你是通过x:Key来引用的吗?用x:Key的话,那个TargetType就可以使用FrameworkElement了,但是从你给的代码中没有看到x:Key
      

  3.   

    你要每个控件都要赋值样式才行,例如:    <Grid>
            <Grid.Resources>
                <Style x:Key="test1" TargetType="{x:Type FrameworkElement}">
                    <Setter Property="Height" Value="30"/>
                    <Setter Property="Width" Value=" 100"/>
                 </Style>
            </Grid.Resources>
            <TextBox HorizontalAlignment="Left" Margin="92,174,0,0" Name="textBox1" VerticalAlignment="Top"
                     Style="{StaticResource ResourceKey=test1}"  />
            <Button Content="Button"  HorizontalAlignment="Left" Margin="292,172,0,0" Name="button1" VerticalAlignment="Top"
                    Style="{StaticResource ResourceKey=test1}" />
        </Grid>
      

  4.   

    不指定具体类型的话为每个控件设置Style="{}"
      

  5.   


    不是通过x:Key。控件是后台动态创建和修改的,根据情况可能先创建一些Rectangle,过一会把这些Rectangle清掉换成Ellipse,等等。我的初衷是在前台写好样式之后,后台直接把控件new出来之后丢进来就行了。现在得多一步,通过Key找到Style赋给生成的控件,这确实也是一个方法。
      

  6.   

    <ItemsControl>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Viewbox Width="60" Height="50" Stretch="Uniform">
                    <ContentControl Content="{Binding}">
                    </ContentControl>
                </Viewbox>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
    试试这个
      

  7.   

    我试了一下。这样做就可以了
    <Grid>
            <StackPanel>
                <StackPanel.Resources>
                    <Style x:Key="SDFSDSF" TargetType ="{x:Type  FrameworkElement}">
                        <Setter Property ="Height" Value="200"/>
                        <Setter Property ="Width" Value="20"/>
                    </Style>
                </StackPanel.Resources>
                <Button Style="{StaticResource SDFSDSF}">
                    SDFSDF
                </Button>
            </StackPanel>
        </Grid>
    你之所以不好使,应该是style对象没有明确吧,强制指定一下就好了。
      

  8.   

    一定要用StackPanel来布局么?如果不是的话可以用WrapPanel<WrapPanel ItemHeight="60" ItemWidth="50">
       ....
    </WrapPanel>
      

  9.   

    LZ的对象都是动态添加的,他不加x:Key肯定是希望所有FrameworkElement全局应用这个Style,这样后台只要Add进去就自动应用了。不过这样的写法用FrameworkElement这种Type好像是不行的,必须明确指明TargetType.
      

  10.   

    动态添加也可以啊,
    Style FrameworkElementStyleTest = this.Resources["FrameworkElementStyle"] as Style;
    Button a = new Button();
    a.Style = FrameworkElementStyleTest;
    FrameworkElementStyle是你style的key,这样就ok。
      

  11.   

    你的空间是动态添加的,style的对象new一个就行,所有空间遍历一边挨个加style就ok。
      

  12.   

    StackPanel本身不提供设置items行为的接口,所以Stackpanel就用错了.
    如果一行c#都不想写,也不想没个都不停的添加style,肯定要用ItemsPanel,8楼说的基本没错,但是不应该设置DataTemplate,而是设置ItemContainerStyle的表现形式
     <ItemsControl>
                <ItemsControl.ItemContainerStyle>
                    <Style TargetType="{x:Type FrameworkElement}">
                        <Setter Property="Width" Value="60" />
                        <Setter Property="Height" Value="50" />
                    </Style>
                </ItemsControl.ItemContainerStyle>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <TextBlock Text="1111" Background="Yellow"/>
                <Button Content="123"/>
                <Rectangle Fill="Black"/>
     </ItemsControl>