0

わかりました、これはトリッキーな状況です。私の WPF プロジェクトには、VoltageChannelView.xaml と VoltageView.Xaml という 2 つの 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>

グリッドを 9 行に分割したGrid.Row="1"に注目してください。

VoltageChannelView :

<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 つのボタンがあり、「バイエルン 2」ボタンをクリックすると、VoltageChannelView のコンテンツをVoltageView Grid.Row ="1"に配置する必要があります。これがトリックです。コンテンツ全体を5回動的に生成する必要があります。つまり、Grid.Row="1" に存在する 9 行では、VoltageChannelView のコンテンツが最初の 5 行に表示され、各行に 1 つずつ表示されます。

「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 に答える