Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey,

i using a datagrid in my wpf application where i add data. This works fine but the display show me only the row without input (see here)

What i'm doing wrong? I search the answer already for weeks

Xaml:
<UserControl x:Class="Log.LogViewerDataGrid"
             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:source="clr-namespace:Log"
             mc:Ignorable="d" 
             x:ClassModifier="internal">
    <Grid>
        <DataGrid AutoGenerateColumns="True" ItemsSource="{Binding}" Name="Datagrid" MouseDoubleClick="DatagridMouseDoubleClick" IsReadOnly="True" CanUserResizeRows="False" RowHeight="24"
                  x:FieldModifier="private">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Source="{Binding (source:LogMessage.Icon)}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Binding="{Binding (source:LogMessage.Type)}" Header="Type"/>
                <DataGridTextColumn Binding="{Binding (source:LogMessage.Date)}" Header="Date"/>
                <DataGridTextColumn Binding="{Binding (source:LogMessage.Message)}" Header="Message"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>


C# code:

C#
namespace Log
{
    #region Includes

    using System.Collections.ObjectModel;
    using System.Windows.Input;

    #endregion

    /// <summary>
    ///     The log viewer data grid.
    /// </summary>
    internal sealed partial class LogViewerDataGrid
    {
        /// <summary>
        ///     Initializes a new instance of the <see cref="LogViewerDataGrid" /> class.
        /// </summary>
        internal LogViewerDataGrid()
        {
            this.LogMessages = new ObservableCollection<LogMessage>();
            this.InitializeComponent();
            this.DataContext = this.LogMessages;

            this.Add(new LogMessage()
                     {
                         Date = "Now",
                         Icon = new System.Uri(@"/Messenger;component/Images/error.png", System.UriKind.Relative),
                         Message = "test",
                         Type = "Error"
                     });
        }

        /// <summary>
        /// Gets or sets the log message collection
        /// </summary>
        private ObservableCollection<LogMessage> LogMessages { get; set; }

        /// <summary>
        /// The add.
        /// </summary>
        /// <param name="item">
        /// The item.
        /// </param>
        internal void Add(LogMessage item)
        {
            if (this.LogMessages.Contains(item) == false)
            {
                this.LogMessages.Add(item);
            }
        }

        /// <summary>
        ///     The clear.
        /// </summary>
        internal void Clear()
        {
            this.LogMessages.Clear();
        }
    }
}


LogMessage code:

C#
namespace Log
{
    #region Includes

    using System;
    using System.ComponentModel;

    #endregion

    /// <summary>
    ///     The log message.
    /// </summary>
    internal sealed class LogMessage : INotifyPropertyChanged
    {
        /// <summary>
        ///     The date.
        /// </summary>
        private string date;

        /// <summary>
        ///     The icon.
        /// </summary>
        private Uri icon;

        /// <summary>
        ///     The message.
        /// </summary>
        private string message;

        /// <summary>
        ///     The type.
        /// </summary>
        private string type;

        #region INotifyPropertyChanged Members

        /// <summary>
        ///     The property changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        /// <summary>
        ///     Gets or sets the message.
        /// </summary>
        internal string Message
        {
            get
            {
                return this.message;
            }

            set
            {
                this.message = value;
                this.NotifyPropertyChanged("Message");
            }
        }

        /// <summary>
        ///     Gets or sets the type.
        /// </summary>
        internal string Type
        {
            get
            {
                return this.type;
            }

            set
            {
                this.type = value;
                this.NotifyPropertyChanged("Type");
            }
        }

        /// <summary>
        ///     Gets or sets the icon.
        /// </summary>
        internal Uri Icon
        {
            get
            {
                return this.icon;
            }

            set
            {
                this.icon = value;
                this.NotifyPropertyChanged("Icon");
            }
        }

        /// <summary>
        ///     Gets or sets the date.
        /// </summary>
        internal string Date
        {
            get
            {
                return this.date;
            }

            set
            {
                this.date = value;
                this.NotifyPropertyChanged("Date");
            }
        }



        /// <summary>
        ///     Compares two log message objects on equality
        /// </summary>
        /// <param name="logMessage1">The first log message object</param>
        /// <param name="logMessage2">The second log message object</param>
        /// <returns>True if objects are equal, else false</returns>
        public static bool operator ==(LogMessage logMessage1, LogMessage logMessage2)
        {
            try
            {
                object logMessage1AsObject = logMessage1;
                object logMessage2AsObject = logMessage2;

                if ((logMessage1AsObject == null && logMessage2AsObject != null)
                    || (logMessage2AsObject == null && logMessage1AsObject != null))
                {
                    return false;
                }

                if (logMessage1AsObject == null)
                {
                    return true;
                }

                if ((logMessage1.Date != logMessage2.Date)
                    || (logMessage1.Message != logMessage2.Message)
                    || (logMessage1.Type != logMessage2.Type)
                    || (logMessage1.Icon != logMessage2.Icon))
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        ///     Compares two log message objects on not equality
        /// </summary>
        /// <param name="logMessage1">The first log message object</param>
        /// <param name="logMessage2">The second log message object</param>
        /// <returns>True if objects are not equal, else false</returns>
        public static bool operator !=(LogMessage logMessage1, LogMessage logMessage2)
        {
            return !(logMessage1 == logMessage2);
        }

        /// <summary>
        /// Compares this log message object with another on equality
        /// </summary>
        /// <param name="otherLogMessage">
        /// Other log message object
        /// </param>
        /// <returns>
        /// True if objects are equal, else false
        /// </returns>
        public override bool Equals(object otherLogMessage)
        {
            return this == otherLogMessage as LogMessage;
        }

        /// <summary>
        ///     Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>A hash code for the current object.</returns>
        public override int GetHashCode()
        {
            return this.Date.GetHashCode() ^ this.Message.GetHashCode() ^ this.Type.GetHashCode() ^ this.Icon.GetHashCode();
        }

        #region Private Helpers

        /// <summary>
        /// The notify property changed.
        /// </summary>
        /// <param name="propertyName">
        /// The property name.
        /// </param>
        private void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}


Please help me!

Regards Tobi
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900