几个数据网格问题。

安倍

会员
已加入
2017年3月16日
留言内容
20
编程经验
Beginner
我有一个绑定到XML文件的Datagrid,效果很好。我有3个问题。在发布此内容之前,我已经阅读了许多教程和指南,并且一无所获。

1) 如果添加图像,它将对所有行重复相同的图像。试图弄清楚如何为每一行设置不同的图像。

2) 我正在尝试在表格的空白行中添加内容,以便用户可以自定义自己的咒语。我不断收到错误消息。

C#:
System.InvalidOperationException: 'Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.'

3) 我正在尝试添加搜索,但它对我不起作用。

提前致谢!

类:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Player_Workbench
{
    public class CustomSpell
    {
        public string Name { get; set; }
        public string Level { get; set; }
        public string Roll { get; set; }
        public string School { get; set; }
        public string CastTime { get; set; }
        public string Range { get; set; }
        public string Target { get; set; }
        public string Component { get; set; }
        public string Concentrate { get; set; }
        public string Duration { get; set; }
        public string Save { get; set; }
        public string DType { get; set; }
        public string Source { get; set; }
        public string Details { get; set; }


    }
}

码:
C#:
        private void addspell_Click(object sender, RoutedEventArgs e)
        {
            CustomSpell tempspell = new CustomSpell();
            tempspell.Name = "";
            tempspell.Level = "";
            tempspell.Roll = "";
            tempspell.School = "";
            tempspell.CastTime = "";
            tempspell.Range = "";
            tempspell.Target = "";
            tempspell.Component = "";
            tempspell.Concentrate = "";
            tempspell.Duration = "";
            tempspell.Save = "";
            tempspell.DType = "";
            tempspell.Source = "";
            tempspell.Details = "";


            spell_grid.Items.Add(tempspell);
        }
XAML:
C#:
 <DataGrid x:Name="spell_grid" IsTextSearchEnabled="True" HorizontalAlignment="Left" Height="657" Margin="0,80,-4.4,0" VerticalAlignment="Top" Width="638" FontFamily="/Player Workbench;component/Fonts/#UnZialish" AutoGenerateColumns="False" DataContext="{Binding Source={StaticResource Spell}}" ItemsSource="{Binding XPath=/SpellInfo/Spell}" CanUserReorderColumns="True" CanUserResizeColumns="False" FontSize="14" MinColumnWidth="1" BorderBrush="{x:Null}" Foreground="#FFB3B3B3" HorizontalGridLinesBrush="#FF565656" VerticalGridLinesBrush="#FF565656" Background= "Transparent" CanUserResizeRows="False" RowHeaderWidth="0" FontWeight="Normal" CanUserAddRows="True" >
                                    <DataGrid.Resources>
                                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FFCAA201"/>
                                    </DataGrid.Resources>
                                    <DataGrid.RowBackground>
                                        <ImageBrush/>
                                    </DataGrid.RowBackground>
                                    <DataGrid.AlternatingRowBackground>
                                        <ImageBrush/>
                                    </DataGrid.AlternatingRowBackground>
                                    <DataGrid.ColumnHeaderStyle>
                                        <Style TargetType="{x:Type DataGridColumnHeader}">
                                            <Setter Property="Background" Value="{StaticResource PrimaryBrush}"/>
                                            <Setter Property="Foreground" Value="{StaticResource PrimaryFont}" />
                                            <Setter Property="HorizontalContentAlignment" Value="Center" />
                                        </Style>
                                    </DataGrid.ColumnHeaderStyle>
                                    <DataGridTemplateColumn Header="Image" Width="SizeToCells" IsReadOnly="True">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <Image Source="Images/paladin_divine_smite.png" />
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>
                                    <DataGrid.Columns>
                                        <DataGridTextColumn Header="Name" Binding="{Binding XPath=Sname}"/>
                                        <DataGridTextColumn Header="Level" Binding="{Binding XPath=Level}" />
                                        <DataGridTextColumn Header="Roll" Binding="{Binding XPath=Roll}" />
                                        <DataGridTextColumn Header="School" Binding="{Binding XPath=School}" />
                                        <DataGridTextColumn Header="CastTime" Binding="{Binding XPath=Ctime}" />
                                        <DataGridTextColumn Header="Range" Binding="{Binding XPath=Range}" />
                                        <DataGridTextColumn Header="Target" Binding="{Binding XPath=Target}" />
                                        <DataGridTextColumn Header="Component" Binding="{Binding XPath=Component}" />
                                        <DataGridTextColumn Header="Concentrate" Binding="{Binding XPath=Concentrate}" />
                                        <DataGridTextColumn Header="Duration" Binding="{Binding XPath=Duration}" />
                                        <DataGridTextColumn Header="Save" Binding="{Binding XPath=Save}" />
                                        <DataGridTextColumn Header="DType" Binding="{Binding XPath=Dtype}" />
                                        <DataGridTextColumn Header="Source" Binding="{Binding XPath=Source}" />
                                        <DataGridTextColumn Header="Details" Binding="{Binding XPath=Detail}" />
                                    </DataGrid.Columns>


                                </DataGrid>
                                <TextBox x:Name="searchspell" HorizontalAlignment="Left" Height="23" Margin="10,26,0,0" TextWrapping="Wrap" Text="Search" VerticalAlignment="Top" Width="232" Background="#FFA6A6A6" />
                                <Button x:Name="addspell" HorizontalAlignment="Left" Margin="298,26,0,0" VerticalAlignment="Top" Width="117" Height="23" Content= "Add Spell" Click="addspell_Click">
                                    <Button.Background>
                                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                            <GradientStop Color="Black" Offset="0"/>
                                            <GradientStop Color="White" Offset="0.64"/>
                                        </LinearGradientBrush>
                                    </Button.Background>
                                </Button>
                            </Grid>
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,501
地点
悉尼,澳大利亚
编程经验
10+
如果您有三个不同的问题,那么您应该确定三个线程,每个线程都有一个标题,以概述该问题。在同一线程中问三个独立的问题会使谈话变得混乱。将每个主题放在一个主题下,将每个主题放在一个主题上对每个人都有帮助。
 
最佳 底部