MultiBinding 및 값 변환기를 사용하여이 작업을 수행 할 수 있습니다. ,로 시작하는 기본 경로에 이미지 소스를 결합하는 multibinding를 사용하여 이미지 이름 :
<ImageBrush>
<ImageBrush.ImageSource>
<MultiBinding Converter="{StaticResource MyConverter}">
<Binding Source="{StaticResource MySettings}" Path="Default.FilePath" />
<Binding Source="ImageName.png"></Binding>
</MultiBinding>
</ImageBrush.ImageSource>
</ImageBrush>
그런 다음
IMultiValueConverter를 구현하고 경로의 두 부분을 결합하여을 생성하는 변환이 필요
이미지를 사용하여 어느 쪽 ImageSourceConverter하거나 새 BitmapImage을 만들어 :
class MyConverter: IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
// Concatenate the values.
string filename = Path.Combine(values[0].ToString(), values[1].ToString());
// You can either use an ImageSourceConverter
// to create your image source from the path.
ImageSourceConverter imageConverter = new ImageSourceConverter();
return imageConverter.ConvertFromString(filename);
// ...or you can create a new bitmap with the combined path.
return new BitmapImage(new Uri(filename, UriKind.RelativeOrAbsolute));
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
// No need to implement the convert back as this will never be used in two way binding.
throw new NotImplementedException();
}
}
를 분명히, 당신은 네임 스페이스를 선언하고 액세스 할 수 있도록 설정 및 컨버터를 호출 한 경우합니다 (XAML에서 CLR 물건에 자원 할 필요가 클래스가 다른 경우, 일치하도록이 클래스를 변경하십시오.)
...
xmlns:local ="clr-namespace:WpfApplication1">
<Window.Resources>
<local:MyConverter x:Key="MyConverter"></local:MyConverter>
<local:MySettings x:Key="MySettings"></local:MySettings>
</Window.Resources>
테스트를 거쳤습니다.
[대체 방법은 ImageSource 속성을 C# 코드의 경로를 결합한 데이터 컨텍스트의 속성에 바인딩하는 것일 수 있지만 데이터 기반 텍스트를 설정 한 방법에 따라 달라 지므로 바람직하지 않을 수 있습니다. 많은 경우.]
Simon 대단히 도움과 조언을 주셔서 감사합니다. 응용 프로그램 설정 (Settings.settings)에 경로를 저장할 때 "MySettings"를 사용하는 방법이 확실하지 않았습니다. Window.Resources "MySettings"제안을 약간 변경하여 이 모든 것이 매력처럼 작동했습니다. 나는 또 다른 질문을한다. 나는 그것을 별도로 게시 할 것이고, 나는 네가 친절하게 나를 도와 줄 수 있기를 바란다. 다시 감사합니다. Bill –
Bill
@Bill : 문제 없습니다. 기꺼이 도와주세요. 물론, 당신이 더 많은 질문을 게시하면 나는 그것을 살펴보고 내가 어떤 용도로도 사용될 수 있는지 알아볼 것입니다. Settings.settings는 기본적으로 똑같습니다. "Settings"대신 "MySettings"클래스의 이름을 변경했습니다. 차이는 없습니다. –