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

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

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

问题

我有一个大的*.xaml文件(我们称其为FromView.xaml),它创建了一个由多个堆栈面板、按钮和标签等组成的视图。这个视图作为一个选项卡显示在主窗口中,还有其他选项卡。

我还有一个摘要视图(我们称其为ToView.xaml),作为一个选项卡,显示每个选项卡中的特定部分。

问题是,我想将FromView添加到ToView中,但我不想显示其中的按钮。我已经设法将FromView显示在ToView中了:

    <!-- ToView.xaml -->
    <Canvas MinHeight="300" MinWidth="750" Margin="2">
        <local:FromView Height="300" Width="750" IsSimplifiedView="True"/>
    </Canvas>

此外,我只需要从ToView中访问FromView中按钮的可见性属性。以下是FromView的一部分代码,以便您看到其中的按钮:

    <StackPanel Grid.Column="2" Width="280" HorizontalAlignment="Left" >
        <Grid Background="{StaticResource QualificationBackground}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="120"/>
                <ColumnDefinition Width="80"/>
                <ColumnDefinition Width="20*"/>
                <ColumnDefinition Width="40"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>

            <!--一些内容-->

            <Label Content="SomeContent"  VerticalAlignment="Center" HorizontalAlignment="Right" Grid.Row="6" Grid.Column="0"/>
            <Button Grid.Row="6" Grid.Column="2" Grid.ColumnSpan="2" Width="60" Height="Auto" HorizontalAlignment="Left" Content="Save"/>

            <!--一些内容-->
        </Grid>
    </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:

&lt;!-- ToView.xaml --&gt;
&lt;Canvas MinHeight=&quot;300&quot; MinWidth=&quot;750&quot; Margin=&quot;2&quot;&gt;
	&lt;local:FromView Height=&quot;300&quot; Width=&quot;750&quot; IsSimplifiedView=&quot;True&quot;/&gt;
&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:

&lt;StackPanel Grid.Column=&quot;2&quot; Width=&quot;280&quot; HorizontalAlignment=&quot;Left&quot; &gt;
	&lt;Grid Background=&quot;{StaticResource QualificationBackground}&quot;&gt;
		&lt;Grid.ColumnDefinitions&gt;
			&lt;ColumnDefinition Width=&quot;120&quot;/&gt;
			&lt;ColumnDefinition Width=&quot;80&quot;/&gt;
			&lt;ColumnDefinition Width=&quot;20*&quot;/&gt;
			&lt;ColumnDefinition Width=&quot;40&quot;/&gt;
		&lt;/Grid.ColumnDefinitions&gt;
		&lt;Grid.RowDefinitions&gt;
			&lt;RowDefinition/&gt;
			&lt;RowDefinition/&gt;
			&lt;RowDefinition/&gt;
			&lt;RowDefinition/&gt;
			&lt;RowDefinition/&gt;
			&lt;RowDefinition/&gt;
			&lt;RowDefinition/&gt;
			&lt;RowDefinition/&gt;
			&lt;RowDefinition/&gt;
		&lt;/Grid.RowDefinitions&gt;

		&lt;!--some stuff--&gt;

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

		&lt;!--some stuff--&gt;
	&lt;/Grid&gt;
&lt;/StackPanel&gt;

答案1

得分: 1

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

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

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

例如:

public class FromView : UserControl
{
    public Visibility SomeButtonVisibility
    {
        get => someButton.Visibility;
        set => someButton.Visibility = value;
    }
}

FromView.xaml:

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

ToView.xaml:

<local:FromView SomeButtonVisibility="Collapsed" />

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

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

private bool _showButtons;
public bool ShowButtons
{
    get => _showButtons;
    set
    {
        _showButtons = value;
        if (_showButtons)
        {
            button1.Visibility = Visibility.Visible;
            button2.Visibility = Visibility.Visible;
            // ...
        }
        else
        {
            button1.Visibility = Visibility.Collapsed;
            button2.Visibility = Visibility.Collapsed;
        }
    }
}
英文:

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

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

public class FromView : UserControl
{
    public Visibility SomeButtonVisibility
    {
       get =&gt; someButton.Visibility;
       set =&gt; someButton.Visibility = value;
  }
}

FromView.xaml:

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

ToView.xaml:

&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.:

private bool _showButtons;
public bool ShowButtons
{
    get =&gt; _showButtons
            set
    {
        _showButtons = value;
        if (_showButtons)
        {
            button1.Visibility = Visibility.Visible;
            button2.Visibility = Visibility.Visible;
            ...
                }
        else
        {
            button1.Visibility = Visibility.Collapsed;
            button2.Visibility = Visibility.Collapsed;
        }
    }
}

答案2

得分: 0

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

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

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

public static readonly DependencyProperty IsButtonsVisibleProperty = 
    DependencyProperty.Register(
        "IsButtonsVisible", typeof(Visibility),
        typeof(FromView)
    );

public Visibility IsButtonsVisible
{
    get => (Visibility)GetValue(IsButtonsVisibleProperty);
    set => SetValue(IsButtonsVisibleProperty, value);
}

FromView中的按钮将如下所示:

<Button Grid.Row="6" Grid.Column="2" Grid.ColumnSpan="2" Width="60" 
   Height="Auto" HorizontalAlignment="Left" Content="Save" Visibility="{Binding 
   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:

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

FromView Code Behind with the Dependency Property :

public static readonly DependencyProperty IsButtonsVisibleProperty = 
     DependencyProperty.Register(
        &quot;IsButtonsVisible&quot;, typeof(Visibility),
         typeof(FromView)
    );

        public Visibility IsButtonsVisible
        {
            get =&gt; (Visibility)GetValue(IsButtonsVisibleProperty);
            set =&gt; SetValue(IsButtonsVisibleProperty, value);
        }

Button in the FromView will look as following:

&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; Visibility={Binding 
   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:

确定