/ / Windows Phoneアプリケーションのリストボックス内にボタンコントロールを動的に追加できない-wpf、silverlight、windows-phone-7.1

Windows Phoneアプリケーションで、リストボックス内にボタンコントロールを動的に追加できない - wpf、silverlight、windows-phone-7.1

別のボタンコントロールを追加しようとしていますリストボックス内のデータとともに、リストボックスアイテムごとに動的にリストボックス内にデータを入力します。これを実現するために、datatemplateタグの下に、ボタンコントロールを配置した新しいスタックパネルを追加しました。今、私はボタンが配置されている特定のスタックパネルを見つけて、それを私の状態に応じて真または偽に見えるようにしようとしています。 そこで、リストボックスの項目をループして制御できるように、目的のスタックパネルを見つけようとしました。ただし、リストボックスの項目を繰り返し処理しているときに「Null参照例外」が発生します。 以下は私のxamlコードで、後者は私のxaml.csコードです。

               <ListBox x:Name="TripList" Height="465" HorizontalAlignment="Left" VerticalAlignment="Top" Width="456" Background="White" Foreground="Blue">
<ListBox.ItemTemplate>
<DataTemplate>

<Border BorderBrush="Black" BorderThickness="0,0,0,4">
<StackPanel Orientation="Vertical" Width="456">
<StackPanel Orientation="Vertical">
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" Width="370" FontSize="24" Text="{Binding PUDetails}"/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="24" Text="{Binding TripStatus}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<StackPanel VerticalAlignment="Top" Orientation="Horizontal" Width="300">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="Conf: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding ConfNumber}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="Est Do Tm: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Right" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding DOTime}"/>
</StackPanel>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Vertical">
<StackPanel x:Name="stacktest" Background="Azure" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="Svc: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding TripService}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="PU: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding PURoute}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="DO: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding DORoute}"/>
</StackPanel>
<StackPanel VerticalAlignment="Top" Orientation="Horizontal">
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="Pax: "/>
<TextBlock FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" TextWrapping="Wrap"  VerticalAlignment="Top" FontSize="18" Text="{Binding PaxDetails}"/>
</StackPanel>
</StackPanel>
</StackPanel>
<StackPanel x:Name="stknotchecked" VerticalAlignment="Top" Orientation="Vertical">

<StackPanel Orientation="Horizontal">
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="300" x:Name="btnaccepttrip" Content="accept trip"></Button>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btnrejecttrip" Content="reject"></Button>
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btnshowmap" Content="show map"></Button>
</StackPanel>
</StackPanel>
<StackPanel x:Name="stkaccepted">
<StackPanel Orientation="Horizontal">
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btndirections" Content="directions"></Button>
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btnflightinfo" Content="flight info"></Button>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btndetails" Content="details"></Button>
<Button Background="Gray" Foreground="CadetBlue" VerticalAlignment="Top" Width="110" x:Name="btnlogtimes" Content="log times"></Button>
</StackPanel>
<StackPanel>
<Button x:Name="btnstatus" Content="set status"></Button>
</StackPanel>
</StackPanel>
</StackPanel>
</Border>

</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>



private void addbtncontrols()
{

foreach (TripsList lst in TripList.Items)
{
ListBoxItem item = TripList.ItemContainerGenerator.ContainerFromItem(lst) as ListBoxItem;
Button stk = FindFirstElementInVisualTree<Button>(item);
var stkitem =(Button)stk.FindName("btndirections");
stkitem.Visibility = Visibility.Collapsed;
}
}

private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
var count = VisualTreeHelper.GetChildrenCount(parentElement);
if (count == 0)
return null;

for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(parentElement, i);

if (child != null && child is T)
{
return (T)child;
}
else
{
var result = FindFirstElementInVisualTree<T>(child);
if (result != null)
return result;

}
}
return null;
}

誰かが私の問題を再愛する方法を教えてもらえますか?前もって感謝します..

回答:

回答№1は0

これらを見つけるためにビジュアルツリーをナビゲートする要素とその可視性を手動で設定することは、この問題を解決するための間違った方法です!データバインディングを使用しているようです。 UIにバインドしているモデルまたはビューモデルでVisibilityタイプのさまざまなプロパティを公開してみませんか。次に、これらをさまざまなUI要素のVisibilityプロパティにバインドして、表示/非表示にすることができます。

余談ですが、スタイルについて学びましょう。XAMLには長くて従うのが難しい多くの繰り返されるスタイルのプロパティ。すべてのTextBlockに適用されるTextBlockスタイルを定義して、質問で繰り返されるコードの多くを削除できるはずです。


回答№2の場合は0

上記の説明から、これに関する詳細情報を提供できますか?

  1. addbtncontrolメソッドをどこで呼び出しましたか?イベントに応答する場合は、次のように言います。アイテムをもう1つ追加する場合は、ViewModel内で実行する必要があります。
  2. 「あなたの状態」:どのような状態ですか?リストボックスアイテムViewModelでモデル化できますか?

私は同じものを実装しました:動的アイテムとカスタムテンプレートを含むリストボックス。また、新しいアイテムを追加する機能も提供します。ですから、あなたが達成したいことやビューモデルを改善したいことについてもっと情報を教えていただければ、私がお手伝いします。