[英]Binding TextBlock text in a new Window?
I've created a warning window to verify delete actions by the user, using Window.ShowDialog and setting the DialogResult. 我已经创建了一个警告窗口,使用Window.ShowDialog并设置DialogResult来验证用户的删除操作。 Everything works fine, except that the warning text doesn't appear in the
TextBlock
and I don't know why. 一切工作正常,除了警告文本未出现在
TextBlock
而且我也不知道为什么。 Here's my Window
: 这是我的
Window
:
<Window x:Class="RoVCo.Windows.VerifyWindow"
....
WindowStyle="None" Padding="10" ResizeMode="NoResize">
<StackPanel>
<TextBlock Height="Auto" Text="{Binding TBText, Mode=OneWay}" Foreground="Yellow" TextWrapping="Wrap" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,0,0">
<Button Content="Cancel" Margin="10,0" Width="50" Click="CancelVerify" />
<Button Content="OK" Width="50" Click="ConfirmVerify" />
</StackPanel>
</StackPanel>
</Window>
And the class: 和班级:
public partial class VerifyWindow : Window
{
public VerifyWindow(string content)
{
InitializeComponent();
_text = content;
}
private string _text = "";
public string TBText { get { return _text; } }
private void CancelVerify(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
this.Close();
}
private void ConfirmVerify(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
this.Close();
}
}
And I call it like such: 我这样称呼它:
var window = new RoVCo.Windows.VerifyWindow("Removing this skill will erase all DP spent on it from all levels. Continue?");
if (window.ShowDialog() == false) return;
为了使用绑定,您需要本地属性是公共的,并且是通知属性或依赖项属性。
You can try this solution. 您可以尝试此解决方案。
Change your VerifyWindow constructor to: 将您的VerifyWindow构造函数更改为:
public VerifyWindow() { InitializeComponent(); 公共VerifyWindow(){InitializeComponent(); }
}
and remove the TBText and _text code. 并删除TBText和_text代码。
Create a new class called VerifyViewModel 创建一个名为VerifyViewModel的新类
public class VerifyViewModel : INotifyPropertyChanged { public VerifyViewModel(string content) { this.TBText = content; 公共类VerifyViewModel:INotifyPropertyChanged {public VerifyViewModel(string content){this.TBText = content; }
}
public string TBText { get; private set; } #region INPC code - can create an abstract base view model class and put this there instead public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) { var handler = this.PropertyChanged; if (handler != null) { handler(this, e); } } #endregion
} }
Call this code as below 如下调用此代码
var viewmodel = new VerifyViewModel ("Removing this skill will erase all DP spent on it from all levels. Continue?"); var viewmodel = new VerifyViewModel(“取消此技能将从所有级别上擦除花费在它上面的所有DP。继续吗?”);
var window = new VerifyWindow { DataContext = viewmodel }; if (window.ShowDialog() == false) return;
If its just a single property that you are using in the Dialog
I think using a DependancyProperty
will be the better option then adding all the INotifyPropertyChanged
logic 如果它只是您在
Dialog
中使用的单个属性,我认为使用DependancyProperty
将是更好的选择,然后添加所有INotifyPropertyChanged
逻辑
public partial class VerifyWindow : Window
{
public VerifyWindow(string content)
{
InitializeComponent();
TBText = content;
}
public static readonly DependencyProperty TBTextProperty =
DependencyProperty.Register("TBText", typeof(string), typeof(VerifyWindow), new UIPropertyMetadata(string.Empty));
public string TBText
{
get { return (string)GetValue(TBTextProperty); }
set { SetValue(TBTextProperty, value); }
}
private void CancelVerify(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
this.Close();
}
private void ConfirmVerify(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
this.Close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.