PDA

View Full Version : نمایش تصاویر کاربران در DATAGRID



amin_sltny
شنبه 24 آبان 1393, 11:06 صبح
سلام بچه ها. من یه دیتا گرید دارم به صورت زیر:


<DataGrid Name="dgUsers"
Margin="0 35 0 0"
Grid.Row="0"
Grid.RowSpan="7"
Grid.Column="0"
Grid.ColumnSpan="7"
AutoGenerateColumns="True" GridLinesVisibility="Vertical"
HeadersVisibility="All" Background="LightGray"
RowBackground="LightYellow"
AlternatingRowBackground="LightBlue" BorderBrush="Gray"
BorderThickness="5" IsReadOnly="True" AreRowDetailsFrozen="True"
FrozenColumnCount="2" CanUserResizeColumns="False"
CanUserSortColumns = "False" HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible" SelectionMode="Extended" AutoGeneratingColumn="dgUsers_AutoGeneratingColumn">


</DataGrid>



که به طریقه زیر پرش می کنم



public class ColumnNameAttribute : System.Attribute
{
public ColumnNameAttribute(string Name) { this.Name = Name; }
public string Name { get; set; }
}
public class DataViewUser : System.Attribute
{
[ColumnName("تصویر مشترک")]
public Uri Image { get; set; }


[ColumnName("کد اشتراک")]
public string Id { get; set; }


[ColumnName("نام")]
public string Name { get; set; }


[ColumnName("نام خانوادگی")]
public string Familly { get; set; }


[ColumnName("نام پدر")]
public string FatherName { get; set; }


[ColumnName("کد ملی")]
public string Idd { get; set; }


[ColumnName("جنسیت")]
public string Sex { get; set; }


[ColumnName("تاریخ تولد")]
public string BirthDay { get; set; }


[ColumnName("شماره تلفن")]
public string Telephone { get; set; }


[ColumnName("تاریخ عضویت")]
public string RegistryDate { get; set; }


[ColumnName("آخرین ورود مشترک")]
public string LastLogin { get; set; }


[ColumnName("نوع اشتراک")]
public string UserType { get; set; }
}


public static List<DataViewUser> LoadUserCollectionData()
{
List<DataViewUser> User_List = new List<DataViewUser>();
Assembly myAssembly = Assembly.GetExecutingAssembly();
Uri DefualtImage = new Uri("pack://application:,,,/" + myAssembly.FullName + ";component/Images/icons/user.png");
try
{
DataTable dt = DataBaseAccess.Select("select CU_ID,CU_Name,CU_Family,CU_FatherName,CU_IID,CU_Se x,CU_birthday,CU_Tel,CU_RegistriedDate,CU_LastLogi n,CU_Type from customer");
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow item in dt.Rows)
{
string imagepath = AppDomain.CurrentDomain.BaseDirectory + @"Data\UserImage\" + item[0].ToString() + ".jpg";
DataViewUser _user = new DataViewUser()
{
Id = item[0].ToString(),
Image = (System.IO.File.Exists(imagepath)) ? new Uri(imagepath) : DefualtImage,
Name = item[1].ToString(),
Familly = item[2].ToString(),
FatherName = item[3].ToString(),
Idd = item[4].ToString(),
Sex = item[5].ToString(),
BirthDay = item[6].ToString(),
Telephone = item[7].ToString(),
RegistryDate = item[8].ToString(),
LastLogin = item[8].ToString(),
UserType = item[8].ToString()
};
User_List.Add(_user);
}
}
}
catch (Exception ex)
{
Log.Write(ex);
}
return User_List;
}




اما تصویر توی دیتاگرید نشون داده نمیشه باید چه کنم؟؟؟



125704

Esikhoob
شنبه 24 آبان 1393, 18:24 عصر
سلام

خوب وقتی نوع ستون "تصویر مشترک" را Uri انتخاب کردید ، نباید انتظار داشته باشید چیزی غیر از Uri (آدرس) در آن ستون ببینید.
به نظرم باید نوعش را Image انتخاب کنید. (کلاس موجود در System.Windows.Controls) و Image اینطوری با Uri در ارتباط است (مثال MSDN برای این کلاس):

// Create Image Element
Image myImage = new Image();
myImage.Width = 200;

// Create source
BitmapImage myBitmapImage = new BitmapImage();

// BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg");

// To save significant application memory, set the DecodePixelWidth or
// DecodePixelHeight of the BitmapImage value of the image source to the desired
// height or width of the rendered image. If you don't do this, the application will
// cache the image as though it were rendered as its normal size rather then just
// the size that is displayed.
// Note: In order to preserve aspect ratio, set DecodePixelWidth
// or DecodePixelHeight but not both.
myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();
//set image source
myImage.Source = myBitmapImage;

amin_sltny
یک شنبه 25 آبان 1393, 08:26 صبح
سلام

خوب وقتی نوع ستون "تصویر مشترک" را Uri انتخاب کردید ، نباید انتظار داشته باشید چیزی غیر از Uri (آدرس) در آن ستون ببینید.
به نظرم باید نوعش را Image انتخاب کنید. (کلاس موجود در System.Windows.Controls) و Image اینطوری با Uri در ارتباط است (مثال MSDN برای این کلاس):

// Create Image Element
Image myImage = new Image();
myImage.Width = 200;

// Create source
BitmapImage myBitmapImage = new BitmapImage();

// BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg");

// To save significant application memory, set the DecodePixelWidth or
// DecodePixelHeight of the BitmapImage value of the image source to the desired
// height or width of the rendered image. If you don't do this, the application will
// cache the image as though it were rendered as its normal size rather then just
// the size that is displayed.
// Note: In order to preserve aspect ratio, set DecodePixelWidth
// or DecodePixelHeight but not both.
myBitmapImage.DecodePixelWidth = 200;
myBitmapImage.EndInit();
//set image source
myImage.Source = myBitmapImage;



دوست عزیز من کاری رو که شما فرمودید را انجام دادم اما الان در ستون "تصویر مشترک" عبارت System.Windows.Controls.Image را می بینم

amin_sltny
دوشنبه 26 آبان 1393, 08:09 صبح
کسی نیست به من کمک کنه. :)

amin_sltny
دوشنبه 26 آبان 1393, 08:33 صبح
کسی نیست به من کمک کنه. :)
بچه ها مشکلم حل شد گفتم روشش را برای شما هم در این قسمت بذارم:

دوتا تغییر در کدهام دادم اول:


<DataGrid Name="dgUsers"
Margin="0 63 0 0"
Grid.Row="0"
Grid.RowSpan="7"
Grid.Column="0"
Grid.ColumnSpan="7"
AutoGenerateColumns="False" GridLinesVisibility="Vertical"
HeadersVisibility="All" Background="LightGray"
RowBackground="LightYellow"
AlternatingRowBackground="LightBlue" BorderBrush="Gray"
BorderThickness="5" IsReadOnly="True" AreRowDetailsFrozen="True"
FrozenColumnCount="2" CanUserResizeColumns="False"
CanUserSortColumns = "False" HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible"
SelectionMode="Extended"
AutoGeneratingColumn="dgUsers_AutoGeneratingColumn" ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="تصویر مشترک" Width="75" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Image}" Width="75" Height="75" RenderOptions.BitmapScalingMode="HighQuality" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="شماره مشترک" Binding="{Binding Id}" />
<DataGridTextColumn Header="نام" Binding="{Binding Name}" />
<DataGridTextColumn Header="نام خانوادگی" Binding="{Binding Family}" />
<DataGridTextColumn Header="نام پدر" Binding="{Binding FatherName}" />
<DataGridTextColumn Header="کدملی" Binding="{Binding Idd}" />
<DataGridTextColumn Header="جنسیت" Binding="{Binding Sex}" />
<DataGridTextColumn Header="تاریخ تولد" Binding="{Binding BirthDay}" />
<DataGridTextColumn Header="تلفن" Binding="{Binding Telephone}" />
<DataGridTextColumn Header="تاریخ ثبت نام" Binding="{Binding RegistryDate}" />
<DataGridTextColumn Header="آخرین ورود" Binding="{Binding LastLogin}" />
<DataGridTextColumn Header="نوع کاربری" Binding="{Binding UserType}" />
</DataGrid.Columns>
</DataGrid>



تغییرات AutoGenerateColumns="False" , ItemsSource="{Binding}" , قطعه کد <DataGrid.Column>.