2012-01-14 11 views
-1

私はリストビューを持ち、画像付きのレコードを持つDataGridも持っています。私の問題は、レコードをリストビューまたはデータグリッドに追加しようとすると、文字列または数値のみを追加できますが、画像を追加することはできません。私はopenfiledialogボックスを通してそれを見つけようとしましたが、DataGridセルに割り当てようとしましたが、イメージを検索すると実行時にImageをどのように割り当てるかわかりません。もう一つの問題は、私はDatagridのイメージをバインドしていますが、私はそれを更新して実行時に追加しようとしています。実行時にListviewとDataGridでlinqをSQLに使用する画像を追加する方法

私の2番目の問題はイメージの更新です。もし誰かがリストビューのレコードを追加したり更新したりすることを知っていれば、テンプレートを使ってDatagridを使ってください。神の袋のために私はそれを試してください。 wpfの専門家が誰かが私の問題を解決することができるあなたの数分を教えてください。

私.xmalファイルには、私の問題は、私は、実行時に画像を更新することはできませんまた、私は他のレコードが、カントの画像を追加することができ、実行時にレコードを追加したいとということです

<Window x:Class="UI.ViewClasses" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:UI" 
    Title="ViewClasses" Height="300" Width="456" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" Loaded="Window_Loaded"> 
    <Window.Resources > 
     <local:ImageConverter x:Key="ImageDataConverter"/> 
    </Window.Resources> 
    <Grid> 
     <my:DataGrid ItemsSource="{Binding}" Name="DataGrid" AutoGenerateColumns="False" Margin="12,51,80,35" SelectionMode="Extended" SelectionUnit="Cell" CanUserReorderColumns="True" CanUserResizeColumns="True" 
      CanUserResizeRows="False" CanUserSortColumns="True" IsReadOnly="False" LoadingRow="DataGrid_LoadingRow" AlternatingRowBackground="LightBlue" 

     Loaded="DataGrid_Loaded"> 
      <my:DataGrid.Columns> 

       <my:DataGridTemplateColumn Header="  Frist Name"> 
        <my:DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <TextBox Text="{Binding Path=FirstName}" 
             Margin="-6,0,-6,0"/> 
         </DataTemplate> 
        </my:DataGridTemplateColumn.CellTemplate> 
       </my:DataGridTemplateColumn> 
       <my:DataGridTemplateColumn Header=" Last Name"> 
        <my:DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <TextBox Text="{Binding Path=LastName}" 
             Margin="-6,0,-6,0"/> 
         </DataTemplate> 
        </my:DataGridTemplateColumn.CellTemplate> 
       </my:DataGridTemplateColumn> 
       <my:DataGridTemplateColumn Header=" Gender"> 
        <my:DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
        <TextBox Text="{Binding Path=Gender}" 
             Margin="-6,0,-6,0"/> 
         </DataTemplate> 
        </my:DataGridTemplateColumn.CellTemplate> 
       </my:DataGridTemplateColumn> 

        <my:DataGridTemplateColumn Header=" GPA"> 
        <my:DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <TextBox Text="{Binding Path=GPA}" 
             Margin="-6,0,-6,0"/> 
         </DataTemplate> 
        </my:DataGridTemplateColumn.CellTemplate> 
       </my:DataGridTemplateColumn> 
        <my:DataGridTemplateColumn Header=" Image"> 
        <my:DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <Image Height="50" Name="image1" Source="{Binding Path=MyImage, Converter={StaticResource ImageDataConverter}}" /> 
         </DataTemplate> 
        </my:DataGridTemplateColumn.CellTemplate> 
       </my:DataGridTemplateColumn> 


      </my:DataGrid.Columns> 
     </my:DataGrid> 
     <Button Height="30" Margin="12,10,0,0" Name="btnAdd" VerticalAlignment="Top" HorizontalAlignment="Left" Width="75" Click="btnAdd_Click">Add</Button> 
     <Button Height="30" Margin="166,10,0,0" Name="btnSave" VerticalAlignment="Top" Click="btnSave_Click" HorizontalAlignment="Left" Width="75">Save</Button> 
     <Button Height="30" HorizontalAlignment="Left" Margin="92,10,0,0" VerticalAlignment="Top" Width="75" Name="btnDelete" Click="btnDelete_Click">Delete</Button> 
     <Button Height="30" HorizontalAlignment="Right" Margin="0,10,112,0" Name="Browses" VerticalAlignment="Top" Width="83" Click="Browses_Click">Brows</Button> 
     <TextBox Height="30" HorizontalAlignment="Right" Margin="0,10,0,0" Name="txtBrowseFile" VerticalAlignment="Top" Width="110" /> 
    </Grid> 
</Window> 

And my .xaml.cs file is 

namespace UI 
{ 
    /// <summary> 
    /// Interaction logic for ViewClasses.xaml 
    /// </summary> 
    public partial class ViewClasses : Window 
    { 
     public ViewClasses() 
     { 
      InitializeComponent(); 
     } private DataClasses1DataContext db = new DataClasses1DataContext(); 

     private BindingListCollectionView CustomerView; 

     private void Window_Loaded(object sender, RoutedEventArgs e) 
     { 
      DataClasses1DataContext db = new DataClasses1DataContext(); 
      var custsInCA = from c in db.Students        
          select c; 

      this.DataContext = custsInCA; 
      this.CustomerView = ((BindingListCollectionView)(CollectionViewSource.GetDefaultView(this.DataContext))); 

     } 

     private void DataGrid_LoadingRow(object sender, Microsoft.Windows.Controls.DataGridRowEventArgs e) 
     { 


     } 

     private void DataGrid_Loaded(object sender, RoutedEventArgs e) 
     { 

     } 

     private void btnSave_Click(object sender, RoutedEventArgs e) 
     { 
      try 
      { 
       this.db.SubmitChanges(); 
       MessageBox.Show("Saved"); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.ToString()); 
      } 
     } 

     private void btnAdd_Click(object sender, RoutedEventArgs e) 
     { 
      Student st = ((Student)(this.CustomerView.AddNew())); 

      st.LastName = "<new>"; 
      this.CustomerView.CommitNew(); 
      this.DataGrid.ScrollIntoView(st); 
     } 

     private void btnDelete_Click(object sender, RoutedEventArgs e) 
     { 
      if ((this.CustomerView.CurrentPosition > -1)) 
      { 
       this.CustomerView.RemoveAt(this.CustomerView.CurrentPosition); 
      } 

     } 

     private void Browses_Click(object sender, RoutedEventArgs e) 
     { 
      Image image1 = new Image(); 
      Microsoft.Windows.Controls.DataGrid df = new Microsoft.Windows.Controls.DataGrid(); 
      Microsoft.Win32.OpenFileDialog fileChooser = new Microsoft.Win32.OpenFileDialog(); 
      fileChooser.Filter = " Image files|*.jpg;*.gif;*.bmp;*.png;;*.jpeg"; 
      Nullable<bool> result = fileChooser.ShowDialog(); 
      if (result == true) 

       try 
       { 

        txtBrowseFile.Text = fileChooser.FileName; 

       } 
       catch { return; } 
      if (txtBrowseFile.Text.Trim().Length != 0) 
      { 
       BitmapImage src = new BitmapImage(); 
       src.BeginInit(); 
       src.UriSource = new Uri(txtBrowseFile.Text.Trim(), UriKind.Relative); 
       src.CacheOption = BitmapCacheOption.OnLoad; 
       src.EndInit(); 

       image1.Source = src; 
      }   
     }  
    } 

    public class ImageConverter : IValueConverter 
    { 

     public object Convert(object value, Type targetType, object parameter, 

     System.Globalization.CultureInfo culture) 
     { 
      System.Data.Linq.Binary binaryData = value as System.Data.Linq.Binary; 
      //System.Data.Linq.Binary binaryData = value;// here there is the first error .How convert BinaryData to Object?? 
      if (binaryData == null) 
      { 
       return null; 
      } 

      byte[] buffer = binaryData.ToArray(); 
      if (buffer.Length == 0) 
      { 
       return null; 
      } 

      BitmapImage res = new BitmapImage(); 
      res.BeginInit(); 
      res.StreamSource = new System.IO.MemoryStream(buffer); 
      res.EndInit(); 
      return res; 
     } 
     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

} 

ですデータグリッド。 wpfにエキスパートがいれば私を再生し、可能ならば私が理解できることを再生してください私は私の国で電気負荷シェーディングの問題があるので、私はあなたにすばやく応答できませんので、可能なら返答ください神の袋私はちょうど高貴な人が私を助けてくれることを望んでいる。

+0

URISourceプロパティで指定されたUriKind.Relativeがあることに気付きました。ファイルダイアログからパスを返す場合、URIは相対パスではなく絶対パスになります。ドライブとディレクトリの構造をルートからファイルに示します。 – CodeWarrior

+0

いいですが私の解決策の問題Dear CodeWarrior –

+0

パスを絶対パスではなく相対パスとして扱っている場合、間違ったパスが解決されます。パスが "C:\ MyPics \ Pic1.png"であると仮定します。それは相対的なパスだと思うので、プログラムが実行されているフォルダから相対的に解決されるので、 "C:\ Program Files(x86)\ MyProgram \ C \ MyPics \ Pic1"という名前になります。 .png "その写真は存在しないので、レンダリングすることはありません。あなたがやっていることがうまくいっているかどうかを知る限り、私はこれまで同様のやり方でそれをやっています。 – CodeWarrior

答えて

0

私は以前にINotifyPropertyChangedに関するいくつかの情報で回答を掲載しています。

how to add data to database and show it in datagrid without restarting program?

あなたは追加のヘルプが必要な場合には、あなたの目的にsatisfacctoryであれば、私に教えてください。私はどうなる

EDIT

ことの一つは、あなたのアイテムで作業している方法を変更しています。

public ObservableCollection<Student> Students = null; 
public CollectionViewSource cvs = new CollectionViewSource; 
public ViewClasses() 
{ 
    cvs.Source = Students; 
    InitializeComponent(); 
} 
private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
    //Open Context 
    DataClasses1DataContext db new DataClasses1DataContext(); 
    //Populate ObservableCollection with objects 
    Students = 
     new ObservableCollection<Student>((from c in db.Students select c).ToList()); 
    //Raise PropertyChangedNotification 
    OnPropertyChanged("Students"); 
} 

そして、XAMLで:私は、一般的に多くのこのようなものを使用

DataClasses1DataContext db = new DataClasses1DataContext(); 
    var custsInCA = from c in db.Students        
     select c; 

    this.DataContext = custsInCA; 
    this.CustomerView = ((BindingListCollectionView)(CollectionViewSource.GetDefaultView(this.DataContext))); 

このように

<my:DataGrid ItemsSource="{Binding cvs}" Name="DataGrid"> 

を、あなたのDataGridのItemsSourceがCollectionViewSourceに直接結合され、CollectionViewSourceですStudentsコレクションによって提供され、Changesコレクションの変更通知は、Studentsコレクションが更新されたときに送信されます。

上記のコードはテストコードにすぎず、スペルや句読点の誤りがあります。私は不幸にも今はバスに乗っていて、私の机ではない。

また、Entity Frameworkを使用していますか?

+0

@ Yaqoob Abbasiあなたが求める他の質問には回答が得られない場合があります。まだ回答をアップvotedしていないか、マークしていません。上記のいずれかが役に立つ場合は、私にお知らせください。そうでない場合は、どうして私が助けてくれるのか教えてください。 – CodeWarrior

+0

Respectable CodeWarrior私は私の別のプロジェクトでこれをやろうとすると、もう一度プロジェクトを再作成しようとすると、私のコードにエラーがあります.1つのプロパティの変更行にNOtImpelmenttationExceptionがあります。あなたに非常に非常に感謝しなければならない –

+0

そして、私は私の学生クラスを使用しようとすると、私は新しいクラスの命名を作成する学生クラスのこの公立クラス学生:INotifyPropertyChanged {publicイベントPropertyChangedEventHandler PropertyChanged; プライベートvoid NotifyPropertyChanged(String info) {if(PropertyChanged!= null) { PropertyChanged(this、new PropertyChangedEventArgs(info)); } }エラーが発生しました。これはタイプwpfApplication3の欠落部分修飾子です –

関連する問題