lee d

I encountered something like this before. I created a new project and that resolved the issue.

this is happening again. but this time around this is a part of something larger and I cannot do the same thing like before(Create a new project)

this is part of XAML of a control. the issue is with the ListBox.

if add this control to a window it works just fine.

add a second instance of this control to the same window, listbox wont show up in the second instance of the control

I tried to get the template and find the listbox, it returns null, but it is actuall there.

remove al the instances of the control and adding again also doesnt solve the problem.

(In essence, I want to be able to add this control any number of times. now it is working correctly when a window has only one instance of this)

Any help is appreciated

Code Snippet

<MyControl>

....

<StackPanel Grid.Row="2" Orientation="Horizontal" Height="26">

<StackPanel.Resources>

<Style TargetType="ListBoxItem">

<Setter Property="SnapsToDevicePixels" Value="true"/>

<Setter Property="OverridesDefaultStyle" Value="true"/>

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="ListBoxItem">

<Border

Name="Border"

SnapsToDevicePixels="true">

<ContentPresenter />

</Border>

<ControlTemplate.Triggers>

<Trigger Property="IsSelected" Value="true">

<Setter TargetName="Border" Property="Background"

Value="Transparent"/>

<Setter Property="Foreground" Value="#2ACFC8"/>

</Trigger>

<Trigger Property="IsEnabled" Value="false">

<Setter Property="Foreground"

Value="{StaticResource DisabledForegroundBrush}"/>

</Trigger>

</ControlTemplate.Triggers>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

</StackPanel.Resources>

<ListBox BorderBrush="{x:Null}" Background="#788CAD" SelectedValuePath="Content" SelectedValue="Sat" IsHitTestVisible="False" x:Name="days" ItemTemplate="{StaticResource dt}" >

<ListBox.Resources>

</ListBox.Resources>

<ListBox.ItemsPanel>

<ItemsPanelTemplate>

<StackPanel Orientation="Horizontal"></StackPanel>

</ItemsPanelTemplate>

</ListBox.ItemsPanel>

<ListBox.Items>

<ListBoxItem>Mon</ListBoxItem>

<ListBoxItem>Tue</ListBoxItem>

<ListBoxItem>Wed</ListBoxItem>

<ListBoxItem>Thu</ListBoxItem>

<ListBoxItem>Fri</ListBoxItem>

<ListBoxItem>Sat</ListBoxItem>

<ListBoxItem>Sun</ListBoxItem>

</ListBox.Items>

</ListBox>

<TextBlock Name="txtAM" TextAlignment="Center" VerticalAlignment="Center" Foreground="black" Margin="5,0,0,0" Text="AM" />

<TextBlock Name="txtPM" VerticalAlignment="Center" Foreground="black" Margin="5,0,0,0" Text="PM" />

</StackPanel>

</MyControl>




Re: Windows Presentation Foundation (WPF) listbox(part of custom control) not showing up

Josh Smith

I tested this out, and did not have a problem getting multiple instances to appear in one window. I had to remove the static resource ref to the ItemTemplate "dt" and the "DisabledForegroundBrush" because those aren't in the XAML you posted. I doubt that has anything to do with it, though.

What version of WPF are you using I tested this out against 3.0 (using VS2005 + "Orcas" extensions).






Re: Windows Presentation Foundation (WPF) listbox(part of custom control) not showing up

lee d

I am using the same version as yours, what I do is try to do a FindName in the template in the code behind, the first time it will find the name in the template but for any instance there after it will just return null. I know it might be difficult to reproduce outside the project I have. any idea what might be going on.

on a side note, any idea what might be happening for the exception mentioned in this post

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=2054092&SiteID=1






Re: Windows Presentation Foundation (WPF) listbox(part of custom control) not showing up

Josh Smith

I tested this out by calling FindName on the MyControl, passing in "days", and it found the ListBox every time. Here's my test code:

Code Snippet

void Window1_Loaded(object sender, RoutedEventArgs e)

{

StackPanel stk = this.Content as StackPanel;

foreach (MyControl myControl in stk.Children)

{

ListBox days = myControl.FindName("days") as ListBox;

Debug.Assert(days != null);

}

}

There are three MyControls in the StackPanel, and the Assert never fails.

Can you use the control's FindName instead of the template's

Regarding the exception you linked to in the other post, I don't know what's up with that.






Re: Windows Presentation Foundation (WPF) listbox(part of custom control) not showing up

lee d

I had to move everything into a UserControl and used the UserControl inside the control. seems to work