我在我的WPF MVVM项目中使用Material Design。我有以下代码:
<materialDesign:DialogHost CloseOnClickAway="True"
Grid.RowSpan="2"
Identifier="root"
Grid.ColumnSpan="3">
<materialDesign:DialogHost.DialogContent>
<StackPanel Margin="20">
<Button Style="{StaticResource MaterialDesignRaisedSecondaryDarkButton}"
Width="160"
FontFamily="{StaticResource ResourceKey=vazir}"
Margin="10,5,5,5"
ToolTip="Resource name: MaterialDesignRaisedSecondaryDarkButton"
Content="Option 1"
IsEnabled="{Binding DataContext.ControlsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" />
<Separator BorderThickness="0.5"
Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
<TextBlock Text="" />
<Button Style="{StaticResource MaterialDesignRaisedSecondaryDarkButton}"
Width="180"
Background="Green"
FontFamily="{StaticResource ResourceKey=vazir}"
Margin="10,5,5,5"
ToolTip="Resource name: MaterialDesignRaisedSecondaryDarkButton"
Content="Option 2"
IsEnabled="{Binding DataContext.ControlsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" />
<Button Style="{StaticResource MaterialDesignRaisedSecondaryDarkButton}"
Width="200"
Background="Green"
FontFamily="{StaticResource ResourceKey=vazir}"
Margin="10,5,5,5"
ToolTip="Resource name: MaterialDesignRaisedSecondaryDarkButton"
Content="Option 3"
IsEnabled="{Binding DataContext.ControlsEnabled, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" />
</StackPanel>
</materialDesign:DialogHost.DialogContent>
<Button x:Name="fileDialogBtn"
Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}"
Content="Show Dialog"
Visibility="Collapsed"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="50,0,0,0" />
</materialDesign:DialogHost>
我想通过Button
作用域之外的另一个Button
调用这个DialogHost
。使用作用域之内的Button
,DialogHost
很容易被调用和显示,但是我想从外部调用它。这可能吗?
2条答案
按热度按时间xnifntxz1#
您需要在ViewModel示例中声明一个布尔属性:
IsDialogOpen
并绑定到DialogHost得IsOpen
属性,然后使用命令使按钮将此属性设置为true.vybvopom2#
如果您的按钮不在
DialogHost
本身内,而是在同一个namescope内,您可以为目标DialogHost
分配一个名称,并在将打开它的Button
中将其指定为CommandTarget
。取得或设定要在其上引发指定命令的项目。
第一个