wpf image控件 twoway 问题

bird_3333 2010-05-21 10:40:12
如何在wpf image 控件上实现twoway。
我需要将对象的src属性绑定image控件,
通过OpenFileDialog 修改image控件的图片,
希望可以保证image控件与src 的binding仍能正常通讯。
即我通过OpenFileDialog 修改了image控件的图片,对象的src属性也自动修改
...全文
394 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bird_3333 2010-05-21
  • 打赏
  • 举报
回复
哈哈,OK
搞定收工吃饭
问题在这里


public static readonly DependencyProperty IconProperty = DependencyProperty.Register(
"Icon",
typeof(String),
typeof(MyImage),
new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions.AffectsRender,
(sender, e) =>
{
(sender as MyImage).Icon = e.NewValue.ToString();
}
)
);

应为FrameworkPropertyMetadataOptions.BindsTwoWayByDefault

//
// 摘要:
// 此依赖项属性的数据绑定的 System.Windows.Data.BindingMode 默认为 System.Windows.Data.BindingMode.TwoWay。
BindsTwoWayByDefault = 256,
bird_3333 2010-05-21
  • 打赏
  • 举报
回复
INotifyPropertyChanged 我已实现。
textbox 这样的控件可以这样做。
image不行
image自身是不可编辑的,而我希望他可以通过OpenFileDialog进行人为的变化
如果通过代码修改image的source,binding就会被清除。其他的对象再使用这个模板image的
binding功能就失效了。
我现在的想法是做myimage:usercontrol 自定义依赖属性icon 将对象的src binding到icon上,

<UserControl x:Class="EditorV2.Control.MyImage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Edit="clr-namespace:EditorV2">
<Button Click="Button_Click" Width="80" Height="80">
<Image Margin="3" Stretch="Fill" x:Name="Image"/>
</Button>
</UserControl>



public static readonly DependencyProperty IconProperty = DependencyProperty.Register(
"Icon",
typeof(String),
typeof(MyImage),
new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions.AffectsRender,
(sender, e) =>
{
(sender as MyImage).Icon = e.NewValue.ToString();
}
)
);
public String Icon
{
get
{
string s = (String)GetValue(IconProperty);
return s;
}
set
{
SetValue(IconProperty, value);
Image.Source = new ImageConverter().Convert(value, Type.GetType("System.Windows.Media.ImageSource"), null, new System.Globalization.CultureInfo("en-US")) as ImageSource;
}
}

private void Button_Click(object sender, RoutedEventArgs e)
{
m_OpenFileDialog.Title = Title;
String IconFloderPath = AppDomain.CurrentDomain.BaseDirectory + Floder;
if (!System.IO.Directory.Exists(IconFloderPath)) System.IO.Directory.CreateDirectory(IconFloderPath);
m_OpenFileDialog.InitialDirectory = IconFloderPath;
m_OpenFileDialog.Filter = String.IsNullOrEmpty(Filter) ? "图片文件|*.jpeg" : Filter;
m_OpenFileDialog.ValidateNames = true;
m_OpenFileDialog.CheckPathExists = true;
m_OpenFileDialog.CheckFileExists = true;
if (m_OpenFileDialog.ShowDialog() == true)
{
Icon = Floder + "\\" + m_OpenFileDialog.SafeFileName;
}
}

myimage中加入image控件,集成OpenFileDialog功能。
但貌似 自定义依赖属性icon 与 src 的twoway banding 不成功
再帮我看看我的思路以及问题的所在!
谢谢了
lon123 2010-05-21
  • 打赏
  • 举报
回复
思路是:
1. 你要继承INotifyPropertyChanged
然后实现接口方法:
#region INotifyPropertyChanged Members

private void NotifyPropertyChanged(string Propertyname)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (PropertyChanged != null)
{
foreach(PropertyChangedEventHandler e in handler.GetInvocationList())
e(this, new PropertyChangedEventArgs(Propertyname));
}
}

public event PropertyChangedEventHandler PropertyChanged;
#endregion

2. 在你要绑定的property这样做
private string _myProperty;
public string myProperty
{
get
{
return _myProperty;
}
set
{
_myProperty= value;
NotifyPropertyChanged("myProperty");
}
}
3.然后在页面上要设值的地方这样绑定
Scr="{Binding myProperty, Mode=Twoway}"
lon123 2010-05-21
  • 打赏
  • 举报
回复
你有用任何模式么?比如MVVM?
lon123 2010-05-21
  • 打赏
  • 举报
回复
哦,那是我误会了
bird_3333 2010-05-21
  • 打赏
  • 举报
回复
算这个我今年总共就4个提问,已结2个。
那个没人给答案
lon123 2010-05-21
  • 打赏
  • 举报
回复
搂主保证结贴,我才给你讲。。。结贴是一种美德。。。

110,530

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧