イメージソースをバインドしようとしていますが、そのことは次のとおりです。 ファイルシステムウォッチャーを持つフォルダがあります - 新しいイメージファイルがフォルダに追加されるたびに - 私は完全なパスを取得していますそのイメージをイメージコントロールのソースにバインドします。 新しいTIFFファイルがフォルダに追加されるたびに、イメージがGUIで自動的に変更されます。 イメージソースをバインドする方法
この
は私がやったことです:XAML:プロパティのセッターで
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="414,159,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
<Image x:Name="img1" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" RenderTransformOrigin="1.604,1.161" Source="{Binding ButtonImage}" />
</Grid>
</Window>
MainWindow.xaml.cs
private ImageSource b;
public ImageSource ButtonImage
{
get { return b; }
set
{
b = value;
}
}
private void button_Click(object sender, RoutedEventArgs e)
{
ButtonImage = new BitmapImage(new Uri(@"C:\Users\x\Desktop\1.tif"));
watch();
}
private void watch()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\Users\x\Desktop\ti";
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
watcher.Filter = "*.tif";
}
private void OnChanged(object source, FileSystemEventArgs e)
{
ButtonImage = new BitmapImage(new Uri(e.FullPath));
}
_ "イメージの完全なパスを取得して、イメージコントロールのソースにバインドしたい" _ - なぜあなたはできないのですか?ターゲットの 'Source'プロパティにバインドされたソースとしてのパスのプロパティを持つビューモデルが必要です。上記の例では、ビューモデルを正しく実装していないと仮定すると、ソースプロパティを更新すると自動的にターゲットプロパティが更新されます。 –
良い[mcve]がなければ、何が間違っているかを知ることは不可能です。しかし、あなたが表示するコードは 'INotifyPropertyChanged'(重複してマークされています)を実装せず、' DataContext'も設定しません。 https://stackoverflow.com/questions/2036518/learning-wpf-and-mvvm、https://stackoverflow.com/questions/997650/i-have-some-questions-about-mvvm-pattern、https: //stackoverflow.com/questions/1405739/mvvm-tutorial-from-start-to-finish、https://stackoverflow.com/questions/7063902/better-way-to-trigger-onpropertychanged、... –
.. 。https://stackoverflow.com/questions/772214/in-mvvm-should-the-viewmodel-or-model-implement-inotifypropertychanged、https://stackoverflow.com/questions/3130491/how-to-avoid-implementing -inotifypropertychanged-手動、https://stackoverflow.com/questions/488587/whats-the-best-way-to-call-inotifypropertychangeds-propertychanged-event、https://stackoverflow.com/questions/857820/big-smart -viewmodels-dumb-views-and-any-model-the-best-mvvm-approach、およびhttps://stackoverflow.com/questions/6789236/how-does-wpf-inotifypropertychanged-work –