如何禁用一个属于在XAML中作为局部添加到另一个视图的视图的按钮?

huangapple go评论67阅读模式
英文:

How can I disable a button that is belongs to a view who is added to another as local in xaml?

问题

  1. 我有一个大的*.xaml文件(我们称其为FromView.xaml),它创建了一个由多个堆栈面板、按钮和标签等组成的视图。这个视图作为一个选项卡显示在主窗口中,还有其他选项卡。
  2. 我还有一个摘要视图(我们称其为ToView.xaml),作为一个选项卡,显示每个选项卡中的特定部分。
  3. 问题是,我想将FromView添加到ToView中,但我不想显示其中的按钮。我已经设法将FromView显示在ToView中了:
  4. <!-- ToView.xaml -->
  5. <Canvas MinHeight="300" MinWidth="750" Margin="2">
  6. <local:FromView Height="300" Width="750" IsSimplifiedView="True"/>
  7. </Canvas>
  8. 此外,我只需要从ToView中访问FromView中按钮的可见性属性。以下是FromView的一部分代码,以便您看到其中的按钮:
  9. <StackPanel Grid.Column="2" Width="280" HorizontalAlignment="Left" >
  10. <Grid Background="{StaticResource QualificationBackground}">
  11. <Grid.ColumnDefinitions>
  12. <ColumnDefinition Width="120"/>
  13. <ColumnDefinition Width="80"/>
  14. <ColumnDefinition Width="20*"/>
  15. <ColumnDefinition Width="40"/>
  16. </Grid.ColumnDefinitions>
  17. <Grid.RowDefinitions>
  18. <RowDefinition/>
  19. <RowDefinition/>
  20. <RowDefinition/>
  21. <RowDefinition/>
  22. <RowDefinition/>
  23. <RowDefinition/>
  24. <RowDefinition/>
  25. <RowDefinition/>
  26. <RowDefinition/>
  27. </Grid.RowDefinitions>
  28. <!--一些内容-->
  29. <Label Content="SomeContent" VerticalAlignment="Center" HorizontalAlignment="Right" Grid.Row="6" Grid.Column="0"/>
  30. <Button Grid.Row="6" Grid.Column="2" Grid.ColumnSpan="2" Width="60" Height="Auto" HorizontalAlignment="Left" Content="Save"/>
  31. <!--一些内容-->
  32. </Grid>
  33. </StackPanel>
英文:

I have a big *.xaml file (let's call it FromView.xaml) that creates a view composed of multiple stack panels with buttons and labels etc.. This view is displayed as a tab in the main window, among others.
I also have a summary view (lets call it ToView.xaml) as a tab, that displays certain parts from each tab. <br/>
Here is the problem, I want to add this FromView to ToView, but I don't want to show the buttons in it. I already managed to combine display FromView in ToView:

  1. &lt;!-- ToView.xaml --&gt;
  2. &lt;Canvas MinHeight=&quot;300&quot; MinWidth=&quot;750&quot; Margin=&quot;2&quot;&gt;
  3. &lt;local:FromView Height=&quot;300&quot; Width=&quot;750&quot; IsSimplifiedView=&quot;True&quot;/&gt;
  4. &lt;/Canvas&gt;

In addition, I just need to access the visibility property of the buttons in the FromView from ToView. Here is a snippet from the FromView, so that you can see the button in it:

  1. &lt;StackPanel Grid.Column=&quot;2&quot; Width=&quot;280&quot; HorizontalAlignment=&quot;Left&quot; &gt;
  2. &lt;Grid Background=&quot;{StaticResource QualificationBackground}&quot;&gt;
  3. &lt;Grid.ColumnDefinitions&gt;
  4. &lt;ColumnDefinition Width=&quot;120&quot;/&gt;
  5. &lt;ColumnDefinition Width=&quot;80&quot;/&gt;
  6. &lt;ColumnDefinition Width=&quot;20*&quot;/&gt;
  7. &lt;ColumnDefinition Width=&quot;40&quot;/&gt;
  8. &lt;/Grid.ColumnDefinitions&gt;
  9. &lt;Grid.RowDefinitions&gt;
  10. &lt;RowDefinition/&gt;
  11. &lt;RowDefinition/&gt;
  12. &lt;RowDefinition/&gt;
  13. &lt;RowDefinition/&gt;
  14. &lt;RowDefinition/&gt;
  15. &lt;RowDefinition/&gt;
  16. &lt;RowDefinition/&gt;
  17. &lt;RowDefinition/&gt;
  18. &lt;RowDefinition/&gt;
  19. &lt;/Grid.RowDefinitions&gt;
  20. &lt;!--some stuff--&gt;
  21. &lt;Label Content=&quot;SomeContent&quot; VerticalAlignment=&quot;Center&quot; HorizontalAlignment=&quot;Right&quot; Grid.Row=&quot;6&quot; Grid.Column=&quot;0&quot;/&gt;
  22. &lt;Button Grid.Row=&quot;6&quot; Grid.Column=&quot;2&quot; Grid.ColumnSpan=&quot;2&quot; Width=&quot;60&quot; Height=&quot;Auto&quot; HorizontalAlignment=&quot;Left&quot; Content=&quot;Save&quot;/&gt;
  23. &lt;!--some stuff--&gt;
  24. &lt;/Grid&gt;
  25. &lt;/StackPanel&gt;

答案1

得分: 1

我只需要访问FromView中按钮的可见性属性,但恐怕在XAML中无法这样做。

  1. <local:FromView someButton.Visibility="Collapsed" />

如果您想要能够从ToView设置位于FromView中的Button的属性,您将需要在FromView类中定义依赖属性或包装属性。

例如:

  1. public class FromView : UserControl
  2. {
  3. public Visibility SomeButtonVisibility
  4. {
  5. get => someButton.Visibility;
  6. set => someButton.Visibility = value;
  7. }
  8. }

FromView.xaml:

  1. <Button x:Name="someButton" Content="Save"/>

ToView.xaml:

  1. <local:FromView SomeButtonVisibility="Collapsed" />

如果要能够绑定到SomeButtonVisibility,它必须定义为依赖属性。

而不是为每个按钮创建包装或依赖属性,您可以定义一个名为"ShowButtons"之类的属性,用于设置FromView中的多个(或所有)按钮的可见性属性,例如:

  1. private bool _showButtons;
  2. public bool ShowButtons
  3. {
  4. get => _showButtons;
  5. set
  6. {
  7. _showButtons = value;
  8. if (_showButtons)
  9. {
  10. button1.Visibility = Visibility.Visible;
  11. button2.Visibility = Visibility.Visible;
  12. // ...
  13. }
  14. else
  15. {
  16. button1.Visibility = Visibility.Collapsed;
  17. button2.Visibility = Visibility.Collapsed;
  18. }
  19. }
  20. }
英文:

>In addition, I just need to access the visibility property of the buttons in the FromView from ToView.

I am afraid you cannot do something like this in XAML.

  1. &lt;local:FromView someButton.Visibility=&quot;Collapsed&quot; /&gt;

If you want to be able to set a property of a Button that is located in FromView from ToView, you will have to define dependency or wrapper properties in the FromView class.

For example:

  1. public class FromView : UserControl
  2. {
  3. public Visibility SomeButtonVisibility
  4. {
  5. get =&gt; someButton.Visibility;
  6. set =&gt; someButton.Visibility = value;
  7. }
  8. }

FromView.xaml:

  1. &lt;Button x:Name=&quot;someButton&quot; Content=&quot;Save&quot;/&gt;

ToView.xaml:

  1. &lt;local:FromView SomeButtonVisibility=&quot;Collapsed&quot; /&gt;

If you want to be able to bind to SomeButtonVisibility, it has to be defined as a dependency property.

Instead of creating a wrapper or dependency property for each button, you could define one property called something like "ShowButtons" that sets the Visibility property of several (or all) buttons in FromView, e.g.:

  1. private bool _showButtons;
  2. public bool ShowButtons
  3. {
  4. get =&gt; _showButtons
  5. set
  6. {
  7. _showButtons = value;
  8. if (_showButtons)
  9. {
  10. button1.Visibility = Visibility.Visible;
  11. button2.Visibility = Visibility.Visible;
  12. ...
  13. }
  14. else
  15. {
  16. button1.Visibility = Visibility.Collapsed;
  17. button2.Visibility = Visibility.Collapsed;
  18. }
  19. }
  20. }

答案2

得分: 0

在FromView UserControl中添加一个依赖属性。例如,可以命名为IsButtonsVisible。现在我们可以如下使用它:

  1. <Canvas MinHeight="300" MinWidth="750" Margin="2">
  2. <local:FromView Height="300" Width="750" IsSimplifiedView="True" IsButtonsVisible="Hidden"/>
  3. </Canvas>

FromView的代码后台使用了该依赖属性:

  1. public static readonly DependencyProperty IsButtonsVisibleProperty =
  2. DependencyProperty.Register(
  3. "IsButtonsVisible", typeof(Visibility),
  4. typeof(FromView)
  5. );
  6. public Visibility IsButtonsVisible
  7. {
  8. get => (Visibility)GetValue(IsButtonsVisibleProperty);
  9. set => SetValue(IsButtonsVisibleProperty, value);
  10. }

FromView中的按钮将如下所示:

  1. <Button Grid.Row="6" Grid.Column="2" Grid.ColumnSpan="2" Width="60"
  2. Height="Auto" HorizontalAlignment="Left" Content="Save" Visibility="{Binding
  3. ElementName=Parent, Path=IsButtonsVisible}"/>

这里的"Parent"被定义为FromView UserControl的名称。

英文:

Add a Dependency Property to the FromView UserControl. For example it can be IsButtonsVisible .
Now we can use it as following:

  1. &lt;Canvas MinHeight=&quot;300&quot; MinWidth=&quot;750&quot; Margin=&quot;2&quot;&gt;
  2. &lt;local:FromView Height=&quot;300&quot; Width=&quot;750&quot; IsSimplifiedView=&quot;True&quot;
  3. IsButtonsVisible=&quot;Hidden&quot;/&gt;
  4. &lt;/Canvas&gt;

FromView Code Behind with the Dependency Property :

  1. public static readonly DependencyProperty IsButtonsVisibleProperty =
  2. DependencyProperty.Register(
  3. &quot;IsButtonsVisible&quot;, typeof(Visibility),
  4. typeof(FromView)
  5. );
  6. public Visibility IsButtonsVisible
  7. {
  8. get =&gt; (Visibility)GetValue(IsButtonsVisibleProperty);
  9. set =&gt; SetValue(IsButtonsVisibleProperty, value);
  10. }

Button in the FromView will look as following:

  1. &lt;Button Grid.Row=&quot;6&quot; Grid.Column=&quot;2&quot; Grid.ColumnSpan=&quot;2&quot; Width=&quot;60&quot;
  2. Height=&quot;Auto&quot; HorizontalAlignment=&quot;Left&quot; Content=&quot;Save&quot; Visibility={Binding
  3. ElementName=Parent,Path=IsButtonsVisible}/&gt;

The "Parent" is defined as the Name of the FromView UserControl.

huangapple
  • 本文由 发表于 2023年2月27日 18:07:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定