0

好吧,这是一个棘手的情况。我的 WPF 项目中有 2 个 xaml 文件,VoltageChannelView.xaml 和 VoltageView.Xaml,在我的 VoltageView.xaml 中,我将网格分为 3 行,如下所示:

电压视图

<Grid Style="{DynamicResource styleBackground}" >
    <Grid.RowDefinitions>
        <RowDefinition Height="70" />
        <RowDefinition />
        <RowDefinition Height="50" />
    </Grid.RowDefinitions>

    <Grid Grid.Row="0" >            
    </Grid>

    <Grid Name="ContentGrid" Grid.Row="1" Height="Auto" Width="Auto">
        <Grid.RowDefinitions>
            <RowDefinition Name="Label11" />
            <RowDefinition Name="Label22" />
            <RowDefinition Name="Label33" />
            <RowDefinition Name="Label44" />
            <RowDefinition Name="Label55" />
            <RowDefinition Name="Label66" />
            <RowDefinition Name="Label77" />
            <RowDefinition Name="Label88" />
            <RowDefinition Name="Label99" />
        </Grid.RowDefinitions>
    </Grid>        

    <Grid Grid.Row="2" >
        <Button Content="Bavaria 2" FontSize="13" Height="25" HorizontalAlignment="Left" Margin="30,0,0,0" Name="RefreshBtn" VerticalAlignment="Center" Width="105" />
        <Button Content="Redhook" FontSize="13" Height="25" HorizontalAlignment="Center" Margin="0,0,0,0" Name="Refresh1Btn" VerticalAlignment="Center" Width="105" />
        <Button Content="Bavaria 1" FontSize="13" Height="25" HorizontalAlignment="Right" Margin="0,0,30,0" Name="Refresh2Btn" VerticalAlignment="Center" Width="105" />
    </Grid>
</Grid>

现在您可以注意到Grid.Row="1"我将网格划分为 9 行。

电压通道视图

<CheckBox Content="On" Grid.Column="3" Height="Auto" HorizontalAlignment="Center" Margin="0" Name="On" VerticalAlignment="Center" />
<Button Content="Set" Grid.Column="1" Height="23" HorizontalAlignment="Center" Margin="85,0,0,0" Name="Set" VerticalAlignment="Center" Width="75" />
<TextBox Grid.Column="1" Height="23" HorizontalAlignment="Center" Margin="0,0,80,0" Name="textBox1" VerticalAlignment="Center" Width="70" />
<Label Content="VDD__Main" Grid.Column="0" Height="15" HorizontalAlignment="Center" Margin="0,0,70,0" Name="VoltageLabel" VerticalAlignment="Center" />
<Label Content="0.0 V" Grid.Column="2" Height="15" HorizontalAlignment="Center" Margin="0" Name="CurrentLabel" VerticalAlignment="Center" />

现在我的 Grid.Row="2" 中有 3 个按钮,我的要求是,当我单击“Bavaria 2”按钮时,我希望将 VoltageChannelView 的内容放在我的VoltageView Grid.Row="1" 中。这是诀窍,它应该动态生成整个内容 5 次。即在 Grid.Row="1" 的 9 行中,VoltageChannelView 的内容应该出现在前 5 行中,每行一个。

在单击“Bavaria1”时,它应该生成内容 8 次,依此类推。基本上WPF中是否可以根据每个按钮单击生成VoltageChannelView“n”次的内容并将其显示在我的VoltageView中??????

4

1 回答 1

1

是的。您可以创建一个VoltageChanelView集合,然后为它创建一个数据模板。我认为这样你更容易展示它。

首先,我真的不明白你想用所有这些网格行等来完成什么。但也许使用列表视图而不是划分为 9 行的网格会使其更简单。

实现这个类...

namespace TestWpf
{
    class VoltageChangeModel : INotifyPropertyChanged
    {
        private int _Voltage = 0;
        private string _ChanelName = "";
        private Brush color = Brushes.Blue;

        public Brush Color
        {
            set
            {

                if (value != color)
                {
                    onPropertyChanged("Color");
                    color = value;
                }
            }
            get
            {
                return color;
            }

        }
        public int Voltage
        {
            set
            {

                if (value != _Voltage)
                {
                    onPropertyChanged("Voltage");
                    _Voltage = value;
                }
            }
            get
            {
                return _Voltage;
            }
        }
        public String ChanelName
        {
            set
            {
                if (value != _ChanelName)
                {
                    onPropertyChanged("ChanelName");
                    _ChanelName = value;
                }
            }

            get
            {
                return _ChanelName;
            }
        }
        public VoltageChangeModel(int Voltage, string ChanelName, Brush Color)
        {
            this.ChanelName = ChanelName;
            this.Voltage = Voltage;
            this.Color = Color;
        }
        public override string ToString()
        {
            return _ChanelName;
        }




        public void onPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

next this clas.... 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace TestWpf
{
    class ChanellList : ObservableCollection<VoltageChangeModel>
    {

    }
}
 in the mainWindow this code  after <window ,,,,,,,,,,,>


    <Grid>
        <ListBox x:Name="myViewChannelList" HorizontalAlignment="Left" Height="161" ItemsSource="{Binding}" Margin="82,38,0,0" VerticalAlignment="Top" Width="187">
            <ListBox.ItemTemplate>
                <DataTemplate >
                    <StackPanel Background="{Binding Path=Color}">
                    <Label Content="{Binding Path=Voltage}" ></Label>
                    <Label Content="{Binding Path=ChanelName}"></Label>

                    </StackPanel>
                </DataTemplate>

            </ListBox.ItemTemplate>

        </ListBox>

    </Grid>
</Window>



and then in code behind set the data context.

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;

namespace TestWpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            ChanellList myChanels = new ChanellList();
            myChanels.Add(new VoltageChangeModel(30,"Channel 1 " , Brushes.Blue ));
            myChanels.Add(new VoltageChangeModel(30, "Channel 1 ", Brushes.Red));
            myViewChannelList.DataContext = myChanels;
        }
    }
}

您可以在此处下载源代码http://mihairadulescu.com/download/TestWpf.rar

于 2012-10-08T10:13:13.457 回答