将复选框名称分配给ObservableCollection <string> </string>中的项目

时间:2014-04-23 12:12:37

标签: c# observablecollection

如何设置每个项目的CheckBox名称值

private void LoadLogsNoTime(string ldate, string ext)
{

   string[] logs = Directory.GetFiles(logPath + ldate, ext);
   logList = new ObservableCollection<String>();
   logList.Clear();

  foreach (string logName in logs)
   {
      string s = logName.Substring(logName.IndexOf(ldate) + ldate.Length + 1);
      int extPos = s.LastIndexOf(".");                    // <- finds the extension
      s = s.Substring(0, extPos);                         // <- removes the extension
      s = s.ToUpper();                                    // <- converts to uppercase

      logList.Add(s);                                     // <- adds the items it finds

    }

    DataContext = this;
}

MainWindow.xaml

<GroupBox Grid.Row="4" Grid.ColumnSpan="3" Header="Logs" BorderBrush="Black" Margin="5 5 5 0" FontWeight="Bold" ScrollViewer.CanContentScroll="True" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" VerticalContentAlignment="Stretch" MinHeight="200" MaxHeight="200">
                    <ScrollViewer VerticalScrollBarVisibility="Visible" VerticalContentAlignment="Stretch">
                        <StackPanel Orientation="Vertical" x:Name="spLogsList">
                            <ListBox x:Name="lbLogs" ItemsSource="{Binding logList}">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <CheckBox Content="{Binding}" ToolTip="{Binding}" Tag="{Binding}"/>

                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                        </StackPanel>
                    </ScrollViewer>
                </GroupBox>

0 个答案:

没有答案