我想添加一個背景圖像到一個文本框,但在textChange事件,圖像消失如預期,但如果我退格或刪除文本中的文本,使它是空的,我拿到了DirectoryNotFoundException
被處死。 和目錄:WPF目錄未找到異常和PresentationCore.pdb未加載
無法找到部分路徑'C:\ myProjectFolder \ bin \ Debug..img \ txtBackground.png'。
的XAML:
<TextBox Name="myTextBox" Width="200" TextChanged="myTextBox_TextChanged">
<TextBox.Background>
<ImageBrush ImageSource="img/txtBackground.png" />
</TextBox.Background>
C#代碼:
private void myTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (myTextBox.Text == "")
{
ImageBrush textImageBrush = new ImageBrush();
textImageBrush.ImageSource =
new BitmapImage(
new Uri(@"..img/txtBackground.png", UriKind.Relative)
);
myTextBox.Background = textImageBrush;
}
else
{
myTextBox.Background = null;
}
}
刪除引用,重新添加它們,建立/清潔解決方案和重建,但沒有。 只有當我嘗試將背景添加到文本框時,纔會出現這些錯誤。
有圖像的img文件夾,它是Debug文件夾嗎? – Nitin
是的,我將img文件夾添加到Debug文件夾。 – Asynchronous
理想情況下,您應該將img文件夾添加到您的項目中,並將圖像添加到BuildAction中作爲資源 – Nitin