我在使用用于C#WPF应用程序的用户界面时遇到了一些意外行为。
悬崖版本是我有3个网格,所有"居住"表格上的相同区域。基于一些切换按钮,这些网格被启用或禁用,并在视图内外淡入。但是,似乎在某些情况下可以使用某些TextBox,而其他的则不能。
以下是我应用的风格:
<!-- Fade in/out style for grid -->
<Style
x:Key="FadeInOutStyle"
TargetType="Grid">
<Style.Triggers>
<Trigger
Property="IsEnabled"
Value="True">
<Trigger.Setters>
<Setter
Property="Panel.ZIndex"
Value="1"/>
</Trigger.Setters>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Opacity"
From="0.0"
To="1.0"
Duration="0:0:0.8"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Opacity"
From="1.0"
To="0.0"
Duration="0:0:0.4"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
<Trigger
Property="IsEnabled"
Value="False">
<Trigger.Setters>
<Setter
Property="Panel.ZIndex"
Value="0"/>
</Trigger.Setters>
</Trigger>
</Style.Triggers>
这种风格似乎很适用,网格按预期淡入和淡出视图。这是&#34;检查过的&#34;启用已检查网格并禁用其他网格的功能。如果你想看到剩下的只是这么说,但它会有点狗和小马表演:
private void IndividualRadio_Checked(object sender, RoutedEventArgs e)
{
this.TwoIndividualGrid.IsEnabled = false;
this.CommercialGrid.IsEnabled = false;
this.IndividualGrid.IsEnabled = true;
}
这是其中一个网格的XAML。它们都只有标签和TextBox控件。它们都以相同的边距和类似的维度开始:
<Grid
Name="IndividualGrid"
IsEnabled="False"
Height="30"
Width="420"
Opacity="0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="10,90,0,0"
Style="{StaticResource FadeInOutStyle}"
Panel.ZIndex="0">
<Label
Content="First Name"
Height="25"
Width="90"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Margin="0,0,0,0"/>
<TextBox
Name="SubjFirstNameBox"
Height="30"
Width="100"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Style="{StaticResource ValidationStyle}"
Margin="100,0,0,0">
<TextBox.Text>
<Binding
Path="SubjFirstName"
Mode="TwoWay">
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<Label
Content="Last Name"
Height="25"
Width="90"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Margin="210,0,0,0"/>
<TextBox
Name="SubjLastNameBox"
Height="30"
Width="100"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Style="{StaticResource ValidationStyle}"
Margin="310,0,0,0">
<TextBox.Text>
<Binding
Path="SubjLastName"
Mode="TwoWay">
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</Grid>
而且,仅仅因为我知道有人可能要求看到它,这里有几个我已经绑定的属性的例子。在这种情况下,First Name / SubjFirstName文本框不起作用,但Last Name / SubjLastName文本框不起作用:
private string subjFirstName;
public string SubjFirstName
{
get { return subjFirstName; }
set { subjFirstName = value; }
}
private string subjLastName;
public string SubjLastName
{
get { return subjLastName; }
set { subjLastName = value; }
}
那么,有没有人看到我在这里搞砸了什么?我假设它必须是堆叠的网格,所以如果有人有更好的方法,我很乐意听到它。我相当肯定我之前已经这样做了,而且我认为禁用和设置面板索引可以解决这个问题。
答案 0 :(得分:0)
Soo,经过更深入的观察,我现在发现了以下可能导致这个错误的事情。
<TextBox Name="SubjLastNameBox" Height="30" Width="100" VerticalAlignment="Top" HorizontalAlignment="Left" Style="{StaticResource ValidationStyle}" Margin="310,0,0,0">
<TextBox.Text>
<Binding Path="SubjLastName" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
正如您所看到的,我添加UpdateSourceTrigger="PropertyChanged"
如果您使用Mode="TwoWay"
,这一点非常重要。另外,如我的评论中所述,您需要将INotifyPropertyChanged
添加到视图模型中。
//using System.ComponentModel;
public class MyClass : INotifyPropertyChanged
{
private string _subjLastName;
///<summary>
/// public lastname property
///</summary>
public string SubjLastName
{
get
{
return _subjLastName;
}
set
{
// The if statement here is importent! So you only raise the notification if the value has changed
if (_subjLastName != value)
{
_subjLastName = value;
OnPropertyChanged("SubjLastName");
}
}
}
#region INotify
#region OnPropertyChanged + Handler
///<summary>
/// PropertyChanged event handler
///</summary>
public event PropertyChangedEventHandler PropertyChanged;
///<summary>
/// Notify the UI for changes
///</summary>
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#endregion
}
编辑:
另一种解决方案可能是:
<Trigger Property="IsEnabled" Value="False">
<Trigger.Setters>
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger.Setters>
</Trigger>
<Trigger Property="IsEnabled" Value="true">
<Trigger.Setters>
<Setter Property="Visibility" Value="Visible"/>
</Trigger.Setters>
</Trigger>
在这里,我使用Property="Visibility"
代替Property="Panel.ZIndex"
,因此如果未使用完全控制,则隐藏完整控件,或者如果使用它则显示。 (你需要稍微调整它,因为你想要显示淡入淡出?如果是这样,你需要在开始淡入淡出之前将其恢复到Visible
。此外,你必须Collapse
之后淡出。)
希望这能解决您的问题。