有没有办法在StackPanel中使用网格进行操作

时间:2018-12-20 15:01:26

标签: c# wpf

我要创建ListBox,并且其中应包含一些元素。 我的代码:

import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from numpy.random import rand
from matplotlib.widgets import RectangleSelector

def onselect(eclick, erelease):
    if switch_variable.get() == 1:
        x1, y1 = eclick.xdata, eclick.ydata
        x2, y2 = erelease.xdata, erelease.ydata
        roi = (x1,y1,x2,y2)
        roi = list(map(int, roi))
        cropped = img[int(roi[1]):int(roi[3]), int(roi[0]):int(roi[2])]
        ax.clear
        ax.imshow(cropped)
        fig.canvas.draw()

def toggle_selector(*args):
    if switch_variable.get() == 0:
        print('RectangleSelector deactivated.')
        toggle_selector.RS.set_active(False)
    elif switch_variable.get() == 1:
        print('RectangleSelector activated.')
        toggle_selector.RS.set_active(True)

root = Tk.Tk()
root_panel = Tk.Frame(root)
root_panel.pack(side="top", fill="both", expand="no")

fig = Figure()
ax = fig.add_subplot(111)
rand_img = rand(10, 5)
img = ax.imshow(rand_img, extent=(1, 2, 1, 2), picker=True)
ax.axis([0, 3, 0, 3])

canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

# ROI BUTTON
switch_variable = Tk.IntVar()
ROIBtn = Tk.Checkbutton(master=root_panel, text='ROI', indicatoron=False, variable=switch_variable, command=toggle_selector)
ROIBtn.pack(side=Tk.LEFT)

toggle_selector.RS = RectangleSelector(ax, onselect, drawtype='box')
toggle_selector()

root.mainloop()

我不能以哪种方式将Grid放入列表框并对其进行处理(例如,我希望有机会将其转换为星形) 我已经尝试过:

<ListBox Name="listBoxQuestion" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding Path=Tweets}" Background="Gray">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>                        
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

1 个答案:

答案 0 :(得分:0)

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="800" Width="800" Background="#FF143761">

    <Grid Width="600" Margin="114,126,78,335">
        <ListBox Name="listBoxQuestion" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding ITEMS}">
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="IsSelected" Value="{Binding Content.IsSelected, Mode=TwoWay, RelativeSource={RelativeSource Self}}"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                                <ContentPresenter/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Width="{Binding WIDTH}">                        
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>                        
                        <StackPanel Grid.Row="0" Orientation="Horizontal">
                            <Label Name="id"       
                            Content="{Binding SURVEY_ID}"       
                            Grid.Row="0" 
                            Grid.Column="0"/>
                            <Label Name="question" 
                            Content="{Binding SURVEY_QUESTION}" 
                            Grid.Row="0" 
                            Grid.Column="1" VerticalAlignment="Stretch" />
                        </StackPanel>                        
                        <StackPanel Grid.Row="2" Orientation="Horizontal">
                            <Polygon Name="star1" Points="{Binding POINTS}" Fill="{Binding FILL_COLOR}" Stroke="{Binding STROKE_COLOR}" Width="{Binding STAR_WIDTH}" Height="{Binding STAR_HEIGHT}" HorizontalAlignment="{Binding H_Alignment}" VerticalAlignment="{Binding V_Alignment}" MouseDown="clicked"/>
                            <Polygon Name="star2" Points="{Binding POINTS}" Fill="{Binding FILL_COLOR}" Stroke="{Binding STROKE_COLOR}" Width="{Binding STAR_WIDTH}" Height="{Binding STAR_HEIGHT}" HorizontalAlignment="{Binding H_Alignment}" VerticalAlignment="{Binding V_Alignment}" MouseDown="clicked"/>
                            <Polygon Name="star3" Points="{Binding POINTS}" Fill="{Binding FILL_COLOR}" Stroke="{Binding STROKE_COLOR}" Width="{Binding STAR_WIDTH}" Height="{Binding STAR_HEIGHT}" HorizontalAlignment="{Binding H_Alignment}" VerticalAlignment="{Binding V_Alignment}" MouseDown="clicked"/>
                            <Polygon Name="star4" Points="{Binding POINTS}" Fill="{Binding FILL_COLOR}" Stroke="{Binding STROKE_COLOR}" Width="{Binding STAR_WIDTH}" Height="{Binding STAR_HEIGHT}" HorizontalAlignment="{Binding H_Alignment}" VerticalAlignment="{Binding V_Alignment}" MouseDown="clicked"/>
                            <Polygon Name="star5" Points="{Binding POINTS}" Fill="{Binding FILL_COLOR}" Stroke="{Binding STROKE_COLOR}" Width="{Binding STAR_WIDTH}" Height="{Binding STAR_HEIGHT}" HorizontalAlignment="{Binding H_Alignment}" VerticalAlignment="{Binding V_Alignment}" MouseDown="clicked"/>
                        </StackPanel>                        
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;

namespace WpfApplication1
{
    public class Item
    {
        public uint width;
        public uint starWidth;
        public uint starHeight;

        public string id;
        public string question;
        public PointCollection points;
        public SolidColorBrush fillColor;
        public SolidColorBrush strokeColor;
        public HorizontalAlignment horizontalAlignment;
        public VerticalAlignment verticalAlignment;

        public uint WIDTH
        {
            get { return width; }
            set { width = value; }
        }
        public uint STAR_WIDTH
        {
            get { return starWidth; }
            set { starWidth = value; }
        }
        public uint STAR_HEIGHT
        {
            get { return starHeight; }
            set { starHeight = value; }
        }
        public string SURVEY_ID
        {
            get { return id; }
            set { id = value; }
        }
        public string SURVEY_QUESTION
        {
            get { return question; }
            set { question = value; }
        }
        public PointCollection POINTS
        {
            get { return points; }
            set { points = value; }
        }
        public SolidColorBrush FILL_COLOR
        {
            get { return fillColor; }
            set { fillColor = value; }
        }
        public SolidColorBrush STROKE_COLOR
        {
            get { return strokeColor; }
            set { strokeColor = value; }
        }
        public HorizontalAlignment H_Alignment
        {
            get { return horizontalAlignment; }
            set { horizontalAlignment = value; }
        }
        public VerticalAlignment V_Alignment
        {
            get { return verticalAlignment; }
            set { verticalAlignment = value; }
        }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            listBoxQuestion.ItemsSource = createItems();
        }

        private List<Item> createItems()
        {
            List<Item> items = new List<Item>();

            items.Add(createItem("1)", "Question 1"));
            items.Add(createItem("2)", "Question 2"));
            items.Add(createItem("3)", "Question 3"));

            return items;
        }
        private Item createItem(string id, string question)
        {
            Item item = new Item();

            item.width = 800;
            item.starHeight = 50;
            item.starWidth = 50;

            item.horizontalAlignment = HorizontalAlignment.Left;
            item.verticalAlignment = VerticalAlignment.Center;            

            item.id = id;
            item.question = question;
            item.points = getPoints();
            item.fillColor = getFillColor("Transparent");
            item.strokeColor = getStrokeColor("Green");

            return item;
        }

        private PointCollection getPoints()
        {
            PointCollection myPointLeftCollection = new PointCollection();
            System.Drawing.PointF[] pts = new System.Drawing.PointF[5];

            double rx = 24 / 2;
            double ry = 24 / 2;
            double cx = 0 + rx;
            double cy = 0 + ry;

            // Start at the top.
            double theta = -Math.PI / 2;
            double dtheta = 4 * Math.PI / 5;
            for (int i = 0; i < 5; i++)
            {
                pts[i] = new System.Drawing.PointF(
                    (float)(cx + rx * Math.Cos(theta)),
                    (float)(cy + ry * Math.Sin(theta)));
                theta += dtheta;
            }

            System.Windows.Point Point1Feedback = new System.Windows.Point(pts[0].X, pts[0].Y);
            System.Windows.Point Point2Feedback = new System.Windows.Point(16, 8);
            System.Windows.Point Point3Feedback = new System.Windows.Point(pts[3].X, pts[3].Y);
            System.Windows.Point Point4Feedback = new System.Windows.Point(18, 14);
            System.Windows.Point Point5Feedback = new System.Windows.Point(pts[1].X, pts[1].Y);
            System.Windows.Point Point6Feedback = new System.Windows.Point(12, 18);
            System.Windows.Point Point7Feedback = new System.Windows.Point(pts[4].X, pts[4].Y);
            System.Windows.Point Point8Feedback = new System.Windows.Point(6, 14);
            System.Windows.Point Point9Feedback = new System.Windows.Point(pts[2].X, pts[2].Y);
            System.Windows.Point Point10Feedback = new System.Windows.Point(9, 8);

            myPointLeftCollection.Add(Point1Feedback);
            myPointLeftCollection.Add(Point2Feedback);
            myPointLeftCollection.Add(Point3Feedback);
            myPointLeftCollection.Add(Point4Feedback);
            myPointLeftCollection.Add(Point5Feedback);
            myPointLeftCollection.Add(Point6Feedback);
            myPointLeftCollection.Add(Point7Feedback);
            myPointLeftCollection.Add(Point8Feedback);
            myPointLeftCollection.Add(Point9Feedback);
            myPointLeftCollection.Add(Point10Feedback);

            return myPointLeftCollection;
        }
        private SolidColorBrush getFillColor(string themeColorInner)
        {
            return (SolidColorBrush)new BrushConverter().ConvertFromString(themeColorInner);
        }
        private SolidColorBrush getStrokeColor(string themeColorBorder)
        {
            return (SolidColorBrush)new BrushConverter().ConvertFromString(themeColorBorder);
        }

        private void clicked(object sender, RoutedEventArgs e)
        {

        }
    }
}