我創建了一個警告窗口來驗證用戶的刪除操作,使用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;
答:我必須在我可以使用代碼背後的屬性之前,將窗口的DataContext設置爲自己。 'DataContext =「{Binding RelativeSource = {RelativeSource Self}}」'對我來說似乎應該自動執行。 – 2013-03-01 01:08:49