我有一個用戶控件設置爲列表框項目的列表框。在這個列表框中,我有幾個網格和兩個按鈕。列表框數據模板和可見狀態的問題
<Grid x:Name="LayoutRoot" HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="120" />
<RowDefinition x:Name="personDetailHolder" Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="92.915" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel Grid.RowSpan="2"
Grid.ColumnSpan="2"
Margin="0,0,-0.042,0"
Orientation="Vertical"
d:LayoutOverrides="Height">
<Grid Height="117" Margin="3,0,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="117" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Rectangle />
<!-- removed for brevity -->
<Button x:Name="btnViewDetails"
Width="106"
Height="24"
Margin="0,0,3,6"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Click="btnDetails_Click"
Foreground="#FFFBCE2F"
Style="{StaticResource ViewDetailsButton}" />
<!-- Detail section -->
<Grid x:Name="personDetail"
Grid.Row="1"
MinHeight="365"
Margin="0,0,5,-376"
VerticalAlignment="Bottom"
RenderTransformOrigin="0.5,0.5"
Visibility="Collapsed">
<Rectangle />
<!-- removed for brevity -->
<Button x:Name="btnCloseDetails"
Width="123"
Height="24"
Margin="0,8,8,0"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Click="btnDetails_Click"
Foreground="#FFFBC42F"
Style="{StaticResource CloseDetailsButton}" />
</Grid>
</Grid>
</StackPanel>
</Grid>
單擊事件btnDetails_Click在代碼中調用一個事件的用戶控制
private void btnDetails_Click(object sender, System.Windows.RoutedEventArgs e)
{
UIPanelControl cp = new UIPanelControl();
cp.CheckDetailPanelStatus(this.personDetail, this.personDetailHolder, this.btnViewDetails, this.btnCloseDetails, this.detailAnimation);
}
其調用該方法的背後
public void CheckDetailPanelStatus(Grid panelName, RowDefinition rowName, Button openButtonName, Button closeButtonName, Storyboard animi)
{
if (panelName.Visibility == System.Windows.Visibility.Visible)
{
panelName.Visibility = System.Windows.Visibility.Collapsed;
rowName.Height = new GridLength(0);
openButtonName.Visibility = System.Windows.Visibility.Visible;
closeButtonName.Visibility = System.Windows.Visibility.Collapsed;
}
else
{
panelName.Visibility = System.Windows.Visibility.Visible;
rowName.Height = new GridLength(380);
openButtonName.Visibility = System.Windows.Visibility.Collapsed;
closeButtonName.Visibility = System.Windows.Visibility.Visible;
animi.Begin();
}
}
這裏的目標是返回給用戶結果列表。用戶可以點擊View additional details按鈕展開第二部分,其中顯示有關所選記錄的更多詳細信息。我正在使用MVVM方法,因此命令會發送給虛擬機以方便第二個記錄羣。由於視圖中包含動畫,可見性等,因此我決定將這些事件保留在視圖代碼中。
此解決方案部分適用於當我點擊按鈕時該部分爲選定的列表框項目擴展。但是,如果向下滾動到其他結果,我會發現其他列表框項目會間歇性地展開。這裏沒有可重複的模式(即每5個項目或每個其他記錄)我也看到了一些情況,如果我選擇第一條記錄並展開它,然後向下滾動到下一條記錄並備份第一條記錄已被重置崩潰。
對於可能導致此問題的原因我很遺憾。
任何人都可以提供任何見解,爲什麼我看到這種情況發生或如何防止這種情況發生?
AbdouMomen,非常感謝指針。我嘗試了視覺狀態和擴展器,都似乎解決了這個問題。爲了簡單起見,我使用了擴展器元素,但我將來肯定會使用VisualStateBehavior。 – rlcrews 2011-06-03 14:50:46
很高興幫助:) – AbdouMoumen 2011-06-03 15:42:24