英文:
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:
<!-- ToView.xaml -->
<Canvas MinHeight="300" MinWidth="750" Margin="2">
<local:FromView Height="300" Width="750" IsSimplifiedView="True"/>
</Canvas>
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:
<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>
<!--some stuff-->
<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"/>
<!--some stuff-->
</Grid>
</StackPanel>
答案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.
<local:FromView someButton.Visibility="Collapsed" />
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 => someButton.Visibility;
set => someButton.Visibility = value;
}
}
FromView.xaml:
<Button x:Name="someButton" Content="Save"/>
ToView.xaml:
<local:FromView SomeButtonVisibility="Collapsed" />
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 => _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:
<Canvas MinHeight="300" MinWidth="750" Margin="2">
<local:FromView Height="300" Width="750" IsSimplifiedView="True"
IsButtonsVisible="Hidden"/>
</Canvas>
FromView Code Behind with the Dependency Property :
public static readonly DependencyProperty IsButtonsVisibleProperty =
DependencyProperty.Register(
"IsButtonsVisible", typeof(Visibility),
typeof(FromView)
);
public Visibility IsButtonsVisible
{
get => (Visibility)GetValue(IsButtonsVisibleProperty);
set => SetValue(IsButtonsVisibleProperty, value);
}
Button in the FromView will look as following:
<Button Grid.Row="6" Grid.Column="2" Grid.ColumnSpan="2" Width="60"
Height="Auto" HorizontalAlignment="Left" Content="Save" Visibility={Binding
ElementName=Parent,Path=IsButtonsVisible}/>
The "Parent" is defined as the Name of the FromView UserControl.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论