[XAML] StackPanel stretch?

Pagina: 1
Acties:

Acties:
  • 0 Henk 'm!

  • dj_vibri
  • Registratie: Oktober 2007
  • Laatst online: 16-09 17:16

dj_vibri

int(e^x) = f(u)^n

Topicstarter
Allen,

ik ben bezig met een WPF applicatie te maken welke gebruik maakt van een database.
Volgende xaml code (een stuk ervan) heb ik als template definition:

code:
1
2
3
4
5
6
7
8
9
<DataTemplate x:Key="ShowConnectionDetail">
            <StackPanel Orientation="Horizontal" Background="Red">
                <Ellipse Style="{StaticResource BrokersCircle}" Width="40" Height="40" Margin="10 10 0 10"/>
                <StackPanel Margin="10" [b]HorizontalAlignment="Stretch"[/b]>
                    <TextBlock Background="Aqua" Text="{Binding Info}"/>
                    <TextBox Background="Blue" AcceptsReturn="True" />
                </StackPanel>
            </StackPanel>
</DataTemplate>


en volgende code voor het 'weergeven' van de content:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 <DockPanel HorizontalAlignment="Stretch" Margin="0,0,0,0" Width="Auto">

        <StatusBar x:Name="panelStatusBar"
                     Width="Auto"
                     Height="25"
                     DockPanel.Dock="Bottom"
                     Background="#ddd"
                     HorizontalAlignment="Stretch">
            <StatusBarItem x:Name="panelStatusBarText" HorizontalContentAlignment="Right" Margin="0 0 5 0">You are using the Northwind Test Application.</StatusBarItem>
        </StatusBar>

        <Menu x:Name="panelMenuTop" Width="Auto" Height="25" DockPanel.Dock="Top">
            <MenuItem Header="File">
                <MenuItem Header="Close" Click="CloseApplication_Click"/>
            </MenuItem>
        </Menu>

        <StackPanel DockPanel.Dock="Left" Width="100" Height="Auto">

            <StackPanel.Background>
                <RadialGradientBrush>
                    <GradientStop Color="#FFF9F3F3" Offset="0"/>
                    <GradientStop Color="#FFD3CFCF" Offset="1"/>
                </RadialGradientBrush>
            </StackPanel.Background>

            <Border Padding="5">
                <StackPanel Name="stckBrokers">             
                </StackPanel>
            </Border>
        </StackPanel>

        <TextBlock x:Name="AreaWelcome"
                     DockPanel.Dock="Top"
                     Text="Welcome to this application."
                     FontSize="14"
                     Padding="10" />

        <StackPanel Orientation="Vertical" DockPanel.Dock="Top" Name="stckDetails">
        
        <Expander Header="Server Details">
            <ListView x:Name="ConnectionResults"                        
                        ItemTemplate="{StaticResource ShowConnectionDetail}" BorderThickness="10" BorderBrush="Tomato">
            </ListView>
        </Expander>
            </StackPanel>
    </DockPanel>


Wat ik nu probeer is het volgende: De listview "connectionresults" zou de hele breedte van de stackpanel moeten nemen (wat nu correct is) maar vervolgens de textblock en textbox (defined in datatemplate) zouden de gehele breedte van deze listview moeten aannemen.

Momenteel wordt de textbox e.d. even groot als de bijhorende stackpanel waar ze inzitten. Nu heb ik al verschillende zaken geprobeerd zoals de eerste stackpanel op een 'horizontalAlignment:stretch' te zetten alsook een fixed value mee te geven maar tevergeefs. Met een fixed value zou het mij wel lukken, maar ik wil alles zo dynamisch mogelijk maken....

Iemand een idee? (en hoop natuurlijk dat het duidelijk is :) )

Last night I lay in bed looking up at the stars in the sky and I thought to myself, where the heck is the ceiling.


Acties:
  • 0 Henk 'm!

  • Niemand_Anders
  • Registratie: Juli 2006
  • Laatst online: 09-07-2024

Niemand_Anders

Dat was ik niet..

Een grid is beter te positioneren (ColumDefinition). Je kunt namelijk column 1 op 60 pixels breedte zetten, terwijl je column 2 op '*' zet. Deze neemt dan automatisch de overige ruimte in. Het WPF grid is erg vergelijkbaar met een HTML tabel.

If it isn't broken, fix it until it is..


Acties:
  • 0 Henk 'm!

  • Niemand_Anders
  • Registratie: Juli 2006
  • Laatst online: 09-07-2024

Niemand_Anders

Dat was ik niet..

Hier ook even een handige utility welke ik ooit geschreven heb. Daarmee kun je vrij eenvoudig de control template van de WPF standaard controls bekijken. Je kunt de template ook eenvoudig gebruiken om bijvoorbeeld een textbox afgeronde hoeken te geven (zoals ook kan met -moz-rounded-corners in css).

Expression trekt deze templates standaard al uit de controls, maar VS heeft deze mogelijkheid helaas niet.

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<Window x:Class="ControlTemplateBrowser.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Control Template Browser" >
    <DockPanel LastChildFill="True" MinHeight="300" >
        <ListBox x:Name="TypeList" DockPanel.Dock="Left" Width="200" SelectionChanged="TypeList_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Grid x:Name="PlaceHolder" Width="0" Height="0" />
        <TextBox x:Name="TemplateSource" MinWidth="500" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Auto" />
    </DockPanel>
</Window>

C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Controls;
using System.Reflection;
using System.Windows.Data;
using System.Windows.Markup;
using System.Xml;

namespace ControlTemplateBrowser
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 
    {
        private readonly Type controlType = typeof (Control);
        private readonly Dictionary<Type, string> templates = new Dictionary<Type, string>();
        public Window1()
        {
            InitializeComponent();

        
            List<Type> derivedTypes = new List<Type>();
            Assembly asm = Assembly.GetAssembly(controlType);
            foreach(Type type in asm.GetExportedTypes())
            {
                if (type.IsSubclassOf(controlType) && !type.IsAbstract && type.IsPublic)
                    derivedTypes.Add(type);
            }

            derivedTypes.Sort(new TypeComparer());
            TypeList.ItemsSource = derivedTypes;
        }

        private void TypeList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Type type = TypeList.SelectedItem as Type;
            if (type == null) return;

            if (!templates.ContainsKey(type))
                AddControlTemplate(type);

            TemplateSource.Text = templates[type];
        }

        private void AddControlTemplate(Type type)
        {
            try
            {
                //create a instance of the selected control
                ConstructorInfo ctor = type.GetConstructor(Type.EmptyTypes);
                Control ctl = ctor.Invoke(null) as Control;

                PlaceHolder.Children.Add(ctl);

                //get template
                ControlTemplate template = ctl.Template;
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                StringBuilder sb = new StringBuilder();
                XmlWriter writer = XmlWriter.Create(sb, settings);
                XamlWriter.Save(template, writer);

                templates[type] = sb.ToString();

                PlaceHolder.Children.Remove(ctl);
            }
            catch (Exception err)
            {
                templates[type] = string.Format("<< Error generating template: {0} >>", err.Message);
            }

        }
    }

    internal class TypeComparer : IComparer<Type>
    {
        public int Compare(Type x, Type y)
        {
            return x.FullName.CompareTo(y.FullName);
        }
    }
}

If it isn't broken, fix it until it is..


Acties:
  • 0 Henk 'm!

  • dj_vibri
  • Registratie: Oktober 2007
  • Laatst online: 16-09 17:16

dj_vibri

int(e^x) = f(u)^n

Topicstarter
thxn both :)

had ondertussen nog even verder geprobeerd en heb er uiteindelijk een 'dockpanel' omheen gegooid en dit blijkt te werken.... nu nog zien om het werkende te krijgen in de datatemplate :)

toch bedankt!!! en het script heb 'k al ergens opgeslagen :)

Last night I lay in bed looking up at the stars in the sky and I thought to myself, where the heck is the ceiling.