0

我是 WPF 的新手,我试图在运行时启动一个 ListBox 值,我只需要以 500 的间隔(即 0、500、1000 等)添加 0-15000 的值。至于现在我正在使用 -

一旦类被初始化,我就可以像这样简单地将值添加到列表中 -

    List<int> intTimeData = new List<int>();

    for (int i = 0; i <= 15000; )
    {
        intTimeData.Add(i);
        i += 500;
    }

    listBoxIntTime.ItemsSource = intTimeData;

问题是它不起作用列表看起来很smooshi:

在此处输入图像描述

……有什么帮助吗?

xaml 代码是 -

<UserControl x:Class="WpfApplication1.IRMore"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
            xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
            xmlns:theme="http://schemas.Xceed.Wpf.Toolkit.Themes"

             mc:Ignorable="d" 
             d:DesignHeight="264" d:DesignWidth="438" Foreground="Gray">
    <Grid Height="257" Width="439">
        <Canvas Width="439" Height="257" Margin="0,0,66,23">
            <Image HorizontalAlignment="Left" Name="image1" Source="Resources\B_10.png" Stretch="Fill" VerticalAlignment="Top" Width="439" Height="257" Canvas.Top="0" Canvas.Left="0"  />
            <Label Content="Image" Height="28" HorizontalAlignment="Left" Margin="22,220,0,0" Name="label5" VerticalAlignment="Top" Width="69" FontSize="12" Canvas.Left="-8" Canvas.Top="-213" />
            <Label Content="Sharpening" Height="28" HorizontalAlignment="Left" Margin="22,220,0,0"  VerticalAlignment="Top" Width="105" FontSize="12" Canvas.Left="-8" Canvas.Top="-172" />
            <Label Content="Polarity" Height="28" HorizontalAlignment="Left" Margin="22,220,0,0"  VerticalAlignment="Top" Width="71" FontSize="12" Canvas.Left="-8" Canvas.Top="-140" />
            <Label Content="DRC" Height="28" HorizontalAlignment="Left" Margin="22,220,0,0"  VerticalAlignment="Top" Width="43" FontSize="12" Canvas.Left="-8" Canvas.Top="-103" />
            <Label Content="Calibration" Height="28" HorizontalAlignment="Left" Margin="22,220,0,0"  VerticalAlignment="Top" Width="105" FontSize="12" Canvas.Left="-8" Canvas.Top="-56" />
            <Label Content="Mirror" Height="28" HorizontalAlignment="Left" Margin="22,220,0,0"  VerticalAlignment="Top" Width="59" FontSize="12" Canvas.Left="205" Canvas.Top="-178" />
            <Label Canvas.Left="227" Canvas.Top="117" Content="Int. time" FontSize="12" Height="28" Width="59" />
            <Label Canvas.Left="232" Canvas.Top="215" Content="Camera Power" FontSize="12" Height="28" Width="101" />
            <Image Source ="Resources\A_16-slider.png" Canvas.Left="341" Canvas.Top="41" Height="25" Name="image2" Stretch="None" Width="52" />
            <Label Canvas.Left="319" Canvas.Top="42" Content="On" FontSize="12" Height="28" Width="24" />
            <Label Canvas.Left="399" Canvas.Top="42" Content="Off" FontSize="12" Height="28" Width="24" />
            <Canvas></Canvas>
            <Image Canvas.Left="124" Canvas.Top="47" Height="25" Name="image3" Source="Resources\A_16-slider.png" Stretch="None" Width="52" />
            <RadioButton AllowDrop="True" Canvas.Left="129" Canvas.Top="51" Content="Switch" FontWeight="Bold" ForceCursor="False" Grid.Row="2" Height="16" Name="radioButtonSharpeningOn" Style="{StaticResource ToggleSwitchStyle}" Width="20" Checked="radioButtonSharpening_Checked">
                <RadioButton.Template>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Canvas>
                            <Image Name="buttonImage" Opacity="0" Source="resources\A_16-round-button.png" />
                        </Canvas>
                        <ControlTemplate.Triggers>
                            <Trigger Property="ToggleButton.IsChecked" Value="true">
                                <Setter Property="UIElement.Opacity" TargetName="buttonImage" Value="1" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </RadioButton.Template>
            </RadioButton>
            <RadioButton AllowDrop="True" Canvas.Left="154" Canvas.Top="51" Content="Switch" FontWeight="Bold" ForceCursor="False" Grid.Row="2" Height="17" Name="radioButtonSharpeningOff" Style="{StaticResource ToggleSwitchStyle}" Width="24" Checked="radioButtonSharpening_Checked">
                <RadioButton.Template>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Canvas>
                            <Image Name="buttonImage" Opacity="0" Source="resources\A_16-round-button.png" />
                        </Canvas>
                        <ControlTemplate.Triggers>
                            <Trigger Property="ToggleButton.IsChecked" Value="true">
                                <Setter Property="UIElement.Opacity" TargetName="buttonImage" Value="1" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </RadioButton.Template>
            </RadioButton>
            <Label Canvas.Left="102" Canvas.Top="49" Content="On" FontSize="12" Height="28" Width="24" />
            <Label Canvas.Left="182" Canvas.Top="49" Content="Off" FontSize="12" Height="28" Width="24" />
            <Canvas Name="DRC" > 
            <Image Canvas.Left="124" Canvas.Top="119" Height="25" Name="image4" Source="Resources\A_16-slider.png" Stretch="None" Width="52" />
                <Label Canvas.Left="100" Canvas.Top="122" Content="On" FontSize="12" Height="28" Width="24" />
                <Label Canvas.Left="180" Canvas.Top="122" Content="Off" FontSize="12" Height="28" Width="24" />
            <RadioButton AllowDrop="True" Canvas.Left="129" Canvas.Top="123" Content="Switch" FontWeight="Bold" ForceCursor="False" Grid.Row="2" Height="22" Name="radioButtonDRCOn" Style="{StaticResource ToggleSwitchStyle}" Width="21" Checked="radioButtonDRC_Checked">
                <RadioButton.Template>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Canvas>
                            <Image Name="buttonImage" Opacity="0" Source="resources\A_16-round-button.png" />
                        </Canvas>
                        <ControlTemplate.Triggers>
                            <Trigger Property="ToggleButton.IsChecked" Value="true">
                                <Setter Property="UIElement.Opacity" TargetName="buttonImage" Value="1" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </RadioButton.Template>
            </RadioButton>
            <RadioButton AllowDrop="True" Canvas.Left="154" Canvas.Top="124" Content="Switch" FontWeight="Bold" ForceCursor="False" Grid.Row="2" Height="21" Name="radioButtonDRCOff" Style="{StaticResource ToggleSwitchStyle}" Width="24" Checked="radioButtonDRC_Checked">
                <RadioButton.Template>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Canvas>
                            <Image Name="buttonImage" Opacity="0" Source="resources\A_16-round-button.png" />
                        </Canvas>
                        <ControlTemplate.Triggers>
                            <Trigger Property="ToggleButton.IsChecked" Value="true">
                                <Setter Property="UIElement.Opacity" TargetName="buttonImage" Value="1" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </RadioButton.Template>
            </RadioButton>
            </Canvas>
            <CheckBox AllowDrop="True" Canvas.Left="84" Canvas.Top="161" Content="Switch" FontWeight="Bold" ForceCursor="False" Grid.Row="2" Height="31" Name="radioButtonCalibration" Style="{StaticResource ToggleSwitchStyle}" Width="148" Checked="radioButtonCalibration_Checked">
                <CheckBox.Template>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Canvas Name="Polarity">
                            <Image Name="buttonImage" Source="resources\A_28-non-selected.png" />
                            <TextBlock VerticalAlignment="Center" FontSize="12" Foreground="White" FontWeight="Normal" HorizontalAlignment="Center" TextWrapping="Wrap" Canvas.Left="10" Canvas.Top="5">1P NUC CALIBRATION</TextBlock>
                        </Canvas>
                        <ControlTemplate.Triggers>
                            <Trigger Property="ToggleButton.IsChecked" Value="true">
                                <Setter Property="UIElement.Opacity" TargetName="buttonImage" Value="1" />
                                <Setter Property="Source" TargetName="buttonImage" Value="Resources\A_28-selected.png" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </CheckBox.Template>
            </CheckBox>
            <CheckBox AllowDrop="True" Canvas.Left="14" Canvas.Top="213" Content="Switch" FontWeight="Bold" ForceCursor="False" Grid.Row="2" Height="31" Name="radioButtonRestoreSetting" Style="{StaticResource ToggleSwitchStyle}" Width="148" Click="radioButtonRestoreSetting_Click">
                <CheckBox.Template>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Canvas Name="Polarity">
                            <Image Name="buttonImage" Source="resources\A_28-non-selected.png" />
                            <TextBlock Canvas.Left="30" Canvas.Top="4" FontSize="12" FontWeight="Normal" Foreground="White" HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center">Restore Setting</TextBlock>
                        </Canvas>
                        <ControlTemplate.Triggers>
                            <Trigger Property="ToggleButton.IsChecked" Value="true">
                                <Setter Property="UIElement.Opacity" TargetName="buttonImage" Value="1" />
                                <Setter Property="Image.Source" TargetName="buttonImage" Value="Resources\A_28-selected.png" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </CheckBox.Template>
            </CheckBox>
            <Canvas Name="OnOff" Height="31" Width="89" Canvas.Top="207" Canvas.Left="330">
                <Image Canvas.Left="18" Canvas.Top="3" Height="25" Name="image5" Source="Resources\A_16-slider.png" Stretch="None" Width="52" />
                <Label Canvas.Left="-2" Canvas.Top="5" Content="On" FontSize="12" Height="28" Width="24" />
                <Label Canvas.Left="70" Canvas.Top="5" Content="Off" FontSize="12" Height="28" Width="24" />
                <RadioButton AllowDrop="True" Canvas.Left="23" Canvas.Top="8" Content="Switch" FontWeight="Bold" ForceCursor="False" Grid.Row="2" Height="23" Name="radioButtoncameraPowerOn" Style="{StaticResource ToggleSwitchStyle}" Width="23" Checked="radioButtonCameraPower_Checked" HorizontalAlignment="Center" VerticalAlignment="Center">
                    <RadioButton.Template>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Canvas>
                        <Image Name="buttonImage" Opacity="0" Source="resources\A_16-round-button.png" />
                    </Canvas>
                    <ControlTemplate.Triggers>
                        <Trigger Property="ToggleButton.IsChecked" Value="true">
                            <Setter Property="UIElement.Opacity" TargetName="buttonImage" Value="1" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </RadioButton.Template>
        </RadioButton>
                <RadioButton AllowDrop="True" Canvas.Left="48" Canvas.Top="8" Content="Switch" FontWeight="Bold" ForceCursor="False" Grid.Row="2" Height="23" Name="radioButtonCameraPowerOff" Style="{StaticResource ToggleSwitchStyle}" Width="23" Checked="radioButtonCameraPower_Checked" HorizontalAlignment="Center" VerticalAlignment="Center">
            <RadioButton.Template>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Canvas>
                        <Image Name="buttonImage" Opacity="0" Source="resources\A_16-round-button.png" />
                    </Canvas>
                    <ControlTemplate.Triggers>
                        <Trigger Property="ToggleButton.IsChecked" Value="true">
                            <Setter Property="UIElement.Opacity" TargetName="buttonImage" Value="1" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </RadioButton.Template>
        </RadioButton>
            </Canvas>
            <!--<xctk:IntegerUpDown Style="{StaticResource myIntUpDownStyle}" FormatString="" Increment="500" Maximum="15000" Minimum="0" Value="0" Name="updownIntTime" Text="0" Canvas.Left="334" Canvas.Top="115" Width="60" DataContextChanged="updownIntTime_DataContextChanged"></xctk:IntegerUpDown>-->
            <Label Canvas.Left="227" Canvas.Top="81" Content="Frame Averaging" FontSize="12" Height="28" Width="92" />
            <ListBox SelectionChanged="listBoxframeAvg_SelectionChanged" Name="listBoxframeAvg" Height="25" Width="60" Canvas.Left="339" Canvas.Top="80" Background="#FF404040" BorderBrush="{x:Null}">
                <ListBoxItem Content="1" Background="#FF404040" BorderBrush="Black" OpacityMask="Black"></ListBoxItem>
                <ListBoxItem Content="2" />
                <ListBoxItem Content="4" />
                <ListBoxItem Content="8" />
                <ListBoxItem Content="16" />
                <ListBoxItem Content="32" />
                <ListBoxItem Content="64" />
                <ListBoxItem />
            </ListBox>
<ListBox SelectionChanged="listBoxIntTime_SelectionChanged" Name="listBoxIntTime" Height="25" Width="60" Canvas.Left="339" Canvas.Top="117" Background="#FF404040" BorderBrush="{x:Null}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="60"/>
                </Grid.ColumnDefinitions>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

            <RadioButton Name="radiobuttonOnMirror" AllowDrop="True" Canvas.Left="347" Canvas.Top="45" Height="25" Style="{StaticResource ToggleSwitchStyle}" Width="30" Content="Switch" ForceCursor="False" FontWeight="Bold" Grid.Row="2" Checked="radiobuttonMirror_Checked">
                <ToggleButton.Template>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Canvas>
                            <Image Name="buttonImage" Opacity="0" Source="resources\A_16-round-button.png" />
                        </Canvas>
                        <ControlTemplate.Triggers>
                            <Trigger Property="ToggleButton.IsChecked" Value="true">
                                <Setter Property="Image.Opacity" TargetName="buttonImage" Value="1" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </ToggleButton.Template>
            </RadioButton>
            <RadioButton Name="radiobuttonOffMirror" AllowDrop="True" Canvas.Left="371" Canvas.Top="46" Height="22" Style="{StaticResource ToggleSwitchStyle}" Width="28" Content="Switch" ForceCursor="False" FontWeight="Bold" Grid.Row="2" Checked="radiobuttonMirror_Checked">
                <ToggleButton.Template>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Canvas>
                            <Image Name="buttonImage" Opacity="0" Source="resources\A_16-round-button.png" />
                        </Canvas>
                        <ControlTemplate.Triggers>
                            <Trigger Property="ToggleButton.IsChecked" Value="true">
                                <Setter Property="Image.Opacity" TargetName="buttonImage" Value="1" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </ToggleButton.Template>
            </RadioButton>
        </Canvas>
        <Canvas Margin="84,68,290,175">
            <RadioButton AllowDrop="True" Canvas.Left="64" Canvas.Top="12" Content="Switch" FontWeight="Bold" ForceCursor="False" Grid.Row="2" Height="25" Name="radioButtonPolarityWhiteHot" Style="{StaticResource ToggleSwitchStyle}" Width="64" HorizontalAlignment="Left" VerticalAlignment="Top" Checked="radioButtonPolarity_Checked">
                <RadioButton.Template>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Canvas Name="Polarity">
                            <Image Name="buttonImage" Source="resources\A_27-non-selected.png" />
                            <TextBlock VerticalAlignment="Center"  FontSize="12" Foreground="White" FontWeight="Normal" HorizontalAlignment="Center" TextWrapping="Wrap" Canvas.Left="4" Canvas.Top="5">White Hot</TextBlock>
                        </Canvas>
                        <ControlTemplate.Triggers>
                            <Trigger Property="ToggleButton.IsChecked" Value="true">
                                <Setter Property="UIElement.Opacity" TargetName="buttonImage" Value="1" />
                                <Setter Property="Source" TargetName="buttonImage" Value="Resources\A_27-selected.png" />

                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </RadioButton.Template>
            </RadioButton>
            <RadioButton AllowDrop="True" Canvas.Left="-2" Canvas.Top="12" Content="Switch" FontWeight="Bold" ForceCursor="False" Grid.Row="2" Height="25" Name="radioButtonPolarityBlackHot" Style="{StaticResource ToggleSwitchStyle}" Width="64" Checked="radioButtonPolarity_Checked">
                <RadioButton.Template>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Canvas Name="Polarity">
                            <Image Name="buttonImage" Source="resources\A_26-non-selected.png" />
                            <TextBlock VerticalAlignment="Center"  FontSize="12" Foreground="White" FontWeight="Normal" HorizontalAlignment="Center" TextWrapping="Wrap" Canvas.Left="10" Canvas.Top="5">Black Hot</TextBlock>
                        </Canvas>
                        <ControlTemplate.Triggers>
                            <Trigger Property="ToggleButton.IsChecked" Value="true">
                                <Setter Property="UIElement.Opacity" TargetName="buttonImage" Value="1" />
                                <Setter Property="Source" TargetName="buttonImage" Value="Resources\A_26-selected.png" />

                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </RadioButton.Template>
            </RadioButton>
            <Canvas Name="INtTime" ></Canvas>
        </Canvas>
    </Grid>
</UserControl>
4

3 回答 3

1

使用 XAML 开发时,您应该使用 ObservableCollection<> 而不是 List<> http://msdn.microsoft.com/en-us/library/ms668604.aspx。为什么?因为每次修改集合以刷新视图时,可观察集合都会通知更改。它应该是这样的:

ObservableCollection<int> collection = new ObservableCollection<int>(); ListBox.DataSource = collection;
for (int i = 0; i <= 15000; )
{
collection.Add(i);
i += 500;
}

现在每次调用 add 方法时,数据都会更新

于 2013-10-09T15:39:52.630 回答
0

更新您的 DataTemplate 以获得一个控件来显示您的值,就像我在下面使用 TextBlock

        <DataTemplate>

                <TextBlock Text="{Binding}"/>

        </DataTemplate>
于 2013-10-09T14:24:01.493 回答
0

如果您有视图模型,则可以使用绑定将数据附加到视图,如下所示:

视图模型.cs

public class ViewModel
{
    public List<int> Items { get; set; }

    public ViewModel( )
    {
        Items = new List<int>( );
        for( int index = 0; index < 15000; index += 500 )
        {
            Items.Add( index );
        }
    }
}

查看.xaml.cs

public partial class View : Window
{
    public View( )
    {
        this.DataContext = new ViewModel( );
    }
}

查看.xaml

<ListBox ItemsSource="{Binding Items}" />
于 2013-10-09T14:24:30.160 回答