如何从代码中设置组件TextBlock Text,我尝试像this.dialog.lbTitle.Text
那样访问它,但是无法从代码访问..
修改:
CustomDialog dialog;
public async void button_Click(object sender, RoutedEventArgs e)
{
this.dialog = (CustomDialog)this.Resources["CustomDialogTest"];
//this.dialog.lbTitle.Text = "Haloo"; --> I like to do something like this
this.dialog.Height = 500;
await this.ShowMetroDialogAsync(dialog);
}
XAML:
...
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
...
<Window.Resources>
<Style x:Key="ButtonOpacity" TargetType="{x:Type Button}">
<Setter Property="Control.Opacity" Value="0.2"></Setter>
</Style>
<Dialog:CustomDialog x:Key="CustomDialogTest"
x:Name="CustomTestDialog">
<Grid Background="Beige" ShowGridLines="False">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Image Height="250" Source="/images/rumus.jpg" Grid.Column="0" Grid.Row="0" Grid.RowSpan="12" Margin="10,10,10,10" />
<TextBlock Grid.Column="1" Grid.Row="0" FontSize="20" Text="Title:" />
<TextBlock Name="lbTitle" Grid.Column="2" Grid.Row="0" FontSize="20" Text="" />
</Grid>
</Dialog:CustomDialog>
</Window.Resources>
答案 0 :(得分:0)
在您的方案中,您不是通过使用分部类定义来共享“Dialog”类。 要查看FramworkElement的子项,您可以使用this。
public async void button_Click(object sender, RoutedEventArgs e)
{
dialog = (CustomDialog)this.Resources["CustomDialogTest"];
// at this point ensure that "lbTitle" is instantiated and part of the dialogs visual tree
var tb = dialog.FindName("lbTitle") as TextBlock;
if(tb != null && dialog != null)
{
tb.Text = "Haloo"; --> I like to do something like this
}
}