2013-03-01 107 views
0

我创建了一个警告窗口来验证用户的删除操作,使用Window.ShowDialog并设置DialogResult。一切工作正常,除了警告文本没有出现在TextBlock,我不知道为什么。这里是我的Window在新窗口中绑定TextBlock文本?

<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> 

和类:

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(); 
    } 
} 

我叫它像这样:

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; 
+0

答:我必须在我可以使用代码背后的属性之前,将窗口的DataContext设置为自己。 'DataContext =“{Binding RelativeSource = {RelativeSource Self}}”'对我来说似乎应该自动执行。 – 2013-03-01 01:08:49

回答

0

为了使用绑定,你需要的本地属性是公共的,以及通知属性或依赖属性。

0

你可以试试这个解决方案。

  1. 您VerifyWindow构造改为:

    公共VerifyWindow(){ 在InitializeComponent (); }

并删除TBText和_text代码。

  1. 创建名为VerifyViewModel

    公共类VerifyViewModel新类:INotifyPropertyChanged的 { 公共VerifyViewModel(字符串内容) { this.TBText =内容; }

    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 
    

    }

  2. 调用此代码如下

    VAR视图模型= 新VerifyViewModel( “删除此技能会删除所有DP上花了所有的水平,是否继续?”) ;

    var window = new VerifyWindow 
        { 
         DataContext = viewmodel 
        }; 
        if (window.ShowDialog() == false) return; 
    
0

如果它只是你正在使用的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(); 
    } 
}