Several DATA Grid Issues.

Abe

Member
Joined
Mar 16, 2017
Messages
20
Programming Experience
Beginner
I have a Datagrid bound to an XML file which works great. I am having 3 issues however. Before posting this, I have gone through many tutorials and guides and keep coming up empty.

1) If I add an image, it repeats the same image for all the rows. Trying to figure out how to set a different image for each row.

2) I am trying to add in blank rows the table so users can custom make their own spells. I keep getting an error.

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

3) I am trying to add in a search, and it is not working for me.

Thanks in advance!

Class:
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; }


    }
}

Code:
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>
 
If you have three separate issues then you should cerate three threads, each with a title that summarises the issue it is dedicated to. Asking three separate questions in the same thread makes for a messy conversation. Keeping each thread to a single topic and each topic to a single thread helps everyone.
 
Back
Top Bottom