1
我有一個很大的問題,並試圖解決了這麼久,現在...綁定更新變量
所以我嘗試做: - 我將按鈕添加到我的Wrapgrid與代碼隱藏文件 - 此按鈕應更改作爲圖像來源的變量
Datenbank database = new Datenbank();
Binding bind = new Binding("ValueGet");
bind.Source = database;
bind.Mode = BindingMode.OneWay;
System.Windows.Controls.Button champbtn = new System.Windows.Controls.Button();
champbtn.Name = "btnAhri";
champbtn.Width = 60;
champbtn.Height = 60;
champbtn.Margin = new Thickness(4);
champbtn.SetBinding(Button.CommandProperty, bind);
champbtn.ToolTip = "Ahri";
champbtn.Content = "Press me";
WrapGrid.Children.Add(champbtn);
此作品。我得到我的Button和它的可點擊。 現在你可以看到我添加了一些命令綁定到我的其他類「Datenbank」,它看起來像這樣:
public class Datenbank : INotifyPropertyChanged
{
private string _Source;
public string ImgSource
{
get { return _Source; }
set
{
_Source = value;
NotifyPropertyChanged("ImgSource");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyname)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
public DelegateCommand ValueGet{ get; set; }
public Datenbank()
{
ValueGet = new DelegateCommand(Ahri);
}
private void Ahri(object sender, EventArgs e)
{
System.Windows.Forms.MessageBox.Show("test");
ImgSource = "Ahri_Square_0.png";
}
}
這裏是我的DelegateCommand類:
public class DelegateCommand : ICommand
{
public delegate void SimpleEventHandler(object sender, EventArgs e);
private SimpleEventHandler _eventHandler;
public DelegateCommand(SimpleEventHandler eventHandler)
{
_eventHandler = eventHandler;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_eventHandler(this, new EventArgs());
}
}
正如你可以看到生成的按鈕應該改變
這個變量綁定到一個imagebox在我的XAML代碼「ImgSource」的字符串:
<Image Height="50" Name="image1" Stretch="Fill" Width="50" Source="{Binding ImgSource, Source={StaticResource database}}" />
這也可以。所以現在我的問題是,當我按下生成的按鈕我的「測試」消息框出現,但圖像不會改變它的來源,我真的不知道如何解決這個問題。
當我手動添加一個按鈕與上面生成的按鈕相同的命令,它工作正常!
<Button Command="{Binding ValueGet,Source={StaticResource database}}">Press ME</Button>
,瞬間改變了圖像源和顯示圖片,但不與generatet之一,這是很重要的!
所以我希望任何人都可以幫助我,因爲我找不到問題。