I am making a prototype of a Silverlight application in Expression Blend 4 and I'm trying to show/hide ListBox/ComboBox items based on boolean values in the data they are bound to. I found an example online which suggested this would work but it doesn't:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
xmlns:local="clr-namespace:PrototypeScreens"
mc:Ignorable="d"
x:Class="PrototypeScreens.Toolbar"
Width="640" Height="31">
<UserControl.Resources>
<local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<ComboBox HorizontalAlignment="Right" Style="{StaticResource ComboBox-Sketch}" VerticalAlignment="Top" Width="32" ItemsSource="{Binding Sites}" >
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid>
<ed:RegularPolygon Fill="{StaticResource BaseBackground-Sketch}" ed:GeometryEffect.GeometryEffect="Sketch" HorizontalAlignment="Left" Height="10" InnerRadius="0.47211" Margin="0,0,0,0" PointCount="5" Stretch="Fill" Stroke="{StaticResource BaseBorder-Sketch}" StrokeThickness="2" UseLayoutRounding="False" Width="10"
Visibility="{Binding IsFavorite, Converter={StaticResource BooleanToVisibilityConverter}}" />
<TextBlock Style="{StaticResource BasicTextBlock-Sketch}" Text="{Binding Name}" HorizontalAlignment="Left" Margin="15,0,0,0" />
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
<!--
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="Visibility" Value="{Binding IsRegistered, Converter={StaticResource BooleanToVisibilityConverter}}" />
</Style>
</ComboBox.ItemContainerStyle>
-->
</ComboBox>
<ListBox Height="29" Margin="0,0,32,0" Style="{StaticResource ListBox-Sketch}" VerticalAlignment="Top" ItemsSource="{Binding Sites}" ScrollViewer.HorizontalScrollBarVisibility="Hidden">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" FlowDirection="RightToLeft" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Style="{StaticResource BasicTextBlock-Sketch}" Text="{Binding Id}" HorizontalAlignment="Left" Margin="0,0,0,0" />
<ed:RegularPolygon Fill="{StaticResource BaseBackground-Sketch}" ed:GeometryEffect.GeometryEffect="Sketch" HorizontalAlignment="Left" Height="10" InnerRadius="0.47211" Margin="20,0,0,0" PointCount="5" Stretch="Fill" Stroke="{StaticResource BaseBorder-Sketch}" StrokeThickness="2" UseLayoutRounding="False" Width="10"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<!--
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Visibility" Value="{Binding IsFavorite, Converter={StaticResource BooleanToVisibilityConverter}}" />
</Style>
</ListBox.ItemContainerStyle>
-->
</ListBox>
</Grid>
</UserControl>
When I try to load the screen with this user control, the app crashes unless I comment out the container styles (as in above code). What am I doing wrong?
NOTE: The visibility of the polygon inside the ComboBoxItem works fine.
UPDATE I included the xaml for the entire usercontrol
UPDATE I got some error details:
Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MDDC; .NET4.0C; .NET4.0E; BRI/2)
Timestamp: Tue, 10 May 2011 15:21:02 UTC
Message: Unhandled Error in Silverlight Application Exception has been thrown by the target of an invocation. at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Reflection.Assembly.CreateInstance(String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at Microsoft.Expression.Prototyping.Workspace.PlayerWindow.InstantiateScreen(String screen, Boolean showImmediately)
at Microsoft.Expression.Prototyping.Workspace.PlayerWindow.TransitionScreens(String from, String to)
at Microsoft.Expression.Prototyping.Navigation.NavigationViewModel.NavigateToScreen(String name, Boolean record)
Line: 1
Char: 1
Code: 0
URI: file:///D:/Projects/Expression/Prototype/Bin/Debug/Default.html
UPDATE: I think I made some progress. It looks like the binding can not be resolved. The ListBox and ComboBox have both ItemsSource="{Binding Sites}" and Sites is of type System.Collections.ObjectModel.ObservableCollection<SitesItem>. SiteItem has properties IsRegistered and IsFavorite.
So I guess the question is: Is it possible to bind to bind to IsRegistered and IsFavorite in a ItemContainerStyle?
WPF already comes with a BooleanToVisibilityConverter out of the box. So simply define it as follows:
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</UserControl.Resources>
Hope this helps
Related
I am recently using OxyPlot to draw charts for my WPF project. I have an issue with the tooltip in a line series. The rectangle box of the tooltip is too close to the point my mouse locates. I am wondering if there is any way to modify the position of the tooltip box and make it further from the cursor. Thank a lot. For example, I would like to move the tooltip box (in the red box) to right a bit.
[Edit]
Following Anu's idea, I added the following code but seems like it not works as expected.
In the xaml.cs file, I added
<UserControl.Resources>
<popups:ScreenPointToOffSetScreenPointConverter x:Key="ScreenPointToOffSetScreenPointConverter"/>
</UserControl.Resources>
And then change my OxyPlot to
<ItemsControl ItemsSource="{Binding AmplitudeDtos}" Margin="0" Name="Amplitude" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid popups:GridUtilities.ItemsPerRow="1" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type dtoModels:SignalDto}">
<oxy:PlotView Height="Auto" Width="Auto" MinHeight="40" MinWidth="100" HorizontalContentAlignment="Stretch"
FontStyle="Normal" FontFamily="Roboto, Microsoft YaHei" FontSize="8" Model="{Binding PlotModel}" Controller="{Binding PlotController}" Padding="8" Margin="0">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding SignalLoadedCommand}" CommandParameter="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<oxy:PlotView.DefaultTrackerTemplate>
<ControlTemplate>
<oxy:TrackerControl Position="{Binding Position, Converter={StaticResource ScreenPointToOffSetScreenPointConverter}}" LineExtents="{Binding PlotModel.PlotArea}">
<oxy:TrackerControl.Content>
<TextBlock Text="{Binding}" />
</oxy:TrackerControl.Content>
</oxy:TrackerControl>
</ControlTemplate>
</oxy:PlotView.DefaultTrackerTemplate>
</oxy:PlotView>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Please note that my OxyPlot is in a DataTemplate.
ScreenPointToOffSetScreenPointConverter class is the exactly the same as in Anu's example.
A relatively simple fix for the requirement would be to modify DefaultTrackerTemplate and use a Custom Converter to change the Position.For example, You could define a ScreenPoint converter as following
public class ScreenPointToOffSetScreenPointConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value is ScreenPoint screenPoint)
{
return new ScreenPoint(screenPoint.X + 50, screenPoint.Y);
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// TODO: Implement
throw new NotImplementedException();
}
}
And then in your XAML
<oxy:PlotView Model="{Binding MyModel}">
<oxy:PlotView.DefaultTrackerTemplate>
<ControlTemplate>
<oxy:TrackerControl Position="{Binding Position,Converter={StaticResource ScreenPointToOffSetScreenPointConverter}}" LineExtents="{Binding PlotModel.PlotArea}">
<TextBlock Text="{Binding}" />
</oxy:TrackerControl>
</ControlTemplate>
</oxy:PlotView.DefaultTrackerTemplate>
</oxy:PlotView>
The Converter would add an Offset to the ScreenPoint position for the Tracker. You could modify the Offset Value to the Converter if it makes sense for your application requirement.
As the title says I get a XamlParseException when I try to run the app. I have only just started learning xaml and am currently following a tutorial from a book called Windows Phone 8 game development here is the exception it is giving me.
System.Windows.Markup.XamlParseException occurred
HResult=-2146233087
Message=The property 'System.Windows.Controls.Panel.Children' is set more than once. [Line: 53 Position: 74]
Source=System.Windows
LineNumber=53
LinePosition=74
StackTrace:
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at SpaceAim3D.Views.MenuPage.InitializeComponent()
at SpaceAim3D.Views.MenuPage..ctor()
InnerException:
here is my code
public partial class MenuPage : PhoneApplicationPage
{
private Dictionary<String, String> m_urls = new Dictionary<string, string>();
public MenuPage()
{
InitializeComponent();
m_urls["play"] = "/Views/GamePage.xaml";
m_urls["ranks"] = "/Views/RanksPage.xaml";
m_urls["map"] = "/Views/MapPage.xaml";
m_urls["world"] = "/Views/WorldPage.xaml";
m_urls["help"] = "/Views/HelpPage.xaml";
m_urls["web"] = "/Views/WebPage.xaml";
m_urls["settings"] = "/Views/SettingsPage.xaml";
}
//This is a common event handler used by all entities on menu page
private void BrdPage_Tap(object sender, GestureEventArgs e)
{
//To get the tag name we case the sender object and get the tag as a string
String page = ((Border)sender).Tag as String;
NavigationService.Navigate(new Uri(m_urls[page], UriKind.Relative));
}
}
and here is my Xaml code
<phone:PhoneApplicationPage
x:Class="SpaceAim3D.Views.MenuPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Landscape" Orientation="Landscape"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="125" />
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="/Assets/asteroid.png" />
</Setter.Value>
</Setter>
</Style>
<Style TargetType="TextBlock" x:Key="ButtonText">
<Setter Property="FontSize" Value="40"/>
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</phone:PhoneApplicationPage.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0"
Style="{StaticResource PhoneTextTitle1Style}">
Space Aim <Bold>3D</Bold>
</TextBlock>
<Grid.Background>
<ImageBrush ImageSource="/Assets/background.png" />
</Grid.Background>
<Border Grid.Row="1" Margin="10,5,536,194" Tap="BrdPage_Tap" Tag="play">
<TextBlock Style="{StaticResource ButtonText}" Text="PLAY!"/>
</Border>
<Border Margin="255,74,291,221" Grid.RowSpan="2" Tap="BrdPage_Tap" Tag="ranks">
<TextBlock Style="{StaticResource ButtonText}" Text="RANKS!" Margin="12,66,10,66" Width="160"/>
</Border>
<Border Grid.Row="1" Margin="12,162,534,37" Tap="BrdPage_Tap" Tag="map">
<TextBlock Style="{StaticResource ButtonText}" Text="MAP"/>
</Border>
<Border Grid.Row="1" Margin="238,189,308,10" Tap="BrdPage_Tap" Tag="world">
<TextBlock Style="{StaticResource ButtonText}" Text="WORLD"/>
</Border>
<Border Grid.Row="1" Margin="420,76,126,123" Tap="BrdPage_Tap" Tag="web">
<TextBlock Style="{StaticResource ButtonText}" Text="WEB"/>
</Border>
<Border Grid.Row="1" Margin="556,148,-10,51" Tap="BrdPage_Tap" Tag="help">
<TextBlock Style="{StaticResource ButtonText}" Text="HELP"/>
</Border>
<Border Margin="503,54,12,241" Grid.RowSpan="2" Tap="BrdPage_Tap" Tag="settings">
<TextBlock Style="{StaticResource ButtonText}" Text="SETTINGS"/>
</Border>
</Grid>
</phone:PhoneApplicationPage>
I moved all the xaml code with border tags above the grid.background and it worked
I have following code:
<DataTemplate DataType="{x:Type abc}">
<StackPanel Orientation="Horizontal">
<CheckBox Margin="0,0,3,0" x:Name="CheckBox">
<CheckBox.IsChecked>
<Binding Path="IsSelected"
Mode="TwoWay">
<Binding.RelativeSource>
<RelativeSource Mode="Template" />
</Binding.RelativeSource>
</Binding>
</CheckBox.IsChecked>
</CheckBox>
<TextBlock Text="{Binding}"
Margin="0,0,10,5" />
<TextBlock Text="{Binding Channel}"
Margin="7,0,0,0"
Visibility="{Binding Path=IsChannelVisible,ElementName=View, Converter={StaticResource BooleanToVisibile}}" />
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Valid}" Value="true">
<Setter TargetName="CheckBox" Property="Foreground" Value="#000000"/>
</DataTrigger>
<DataTrigger Binding="{Binding Valid}" Value="false">
<Setter TargetName="CheckBox" Property="Foreground" Value="#999999"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
What i am trying to achieve is change the color of the the checkbox text. But above code is not working. Can anyone please help me with this?
Tried code from Tilak:
<ControlTemplate x:Key="Template"
TargetType="ListViewItem">
<StackPanel Orientation="Horizontal">
<CheckBox Margin="7,0,0,0" Foreground={Binding Valid, Converter={StaticResource ValidToColorConverter}}
x:Name="CheckBox">
<CheckBox.IsChecked>
<Binding Path="IsSelected"
Mode="TwoWay">
<Binding.RelativeSource>
<RelativeSource Mode="Parent" />
</Binding.RelativeSource>
</Binding>
</CheckBox.IsChecked>
</CheckBox>
<ContentPresenter />
<DataTemplate.CheckBox>
<Intellivu:ValidToColorConverter x:Key="ValidToColorConverter" />
</DataTemplate.CheckBox>
</StackPanel>
</ControlTemplate>
This is not working? Am i doing something wrong?
You can use converter and bind to Foreground property directly.
Following following steps for it
Create ValidToColorConverter
public class ValidToColorConverter : IValueConverter
{
// This converts the DateTime object to the string to display.
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
bool valid = value as bool;
return valid ? Colors.Black : new SolidColorBrush(Colors.FromArgb(255,99,99,99));
}
// No need to implement converting back on a one-way binding
public object ConvertBack(object value, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Add ValidToColorConverter as static resource in the resources corresponding to control
<DataTemplate.Resources>
<local:ValidToColorConverter x:Key="ValidToColorConverter" />
</DataTemplate.Resources>
Note - local should be added into page namespaces(corresponding to namespace where converter is defined)
Refer to Converter in the Checkbox
<CheckBox Margin="0,0,3,0" Foreground={Binding Valid, Converter={StaticResource ValidToColorConverter}} x:Name="CheckBox">
I want to use my surfaceslider control inside tagvisualization so that it responds to finger touch when an object is placed, but when I run the program it doesnt moves at all. Can anybody give me pointers (I am new to this sdk).
<s:TagVisualization x:Class="ControlsBox.TagVisual"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="http://schemas.microsoft.com/surface/2008"
Loaded="TagVisual_Loaded">
<Grid>
<!--ProgressBar Margin="50" HorizontalAlignment="Center" Value=""-->
<TextBlock Height="23" HorizontalAlignment="Center" Margin="0" Name="textBlock1" Text="" VerticalAlignment="Top" Padding="0,6,0,0" Foreground="#FFAA4A4A" />
<s:SurfaceSlider Width="100" Height="20" HorizontalAlignment="Center" VerticalAlignment="Top" Padding="0,50,0,0" Value="60" IsSelectionRangeEnabled="False" Margin="20" Name="mySlider" Maximum="100" Interval="1" ValueChanged="mySlider_ValueChanged">
<s:SurfaceSlider.Effect>
<BlurEffect Radius="2"/>
</s:SurfaceSlider.Effect>
</s:SurfaceSlider>
<Ellipse Width="200" Height="200" Stroke="White" StrokeThickness="8" Opacity="0.7">
<Ellipse.Effect>
<BlurEffect Radius="12" />
</Ellipse.Effect>
</Ellipse>
<Ellipse Height="196" Width="196" Stroke="AntiqueWhite" StrokeThickness="4" >
<Ellipse.Effect>
<BlurEffect Radius="4"/>
</Ellipse.Effect>
</Ellipse>
<Label Content="John's Phone" HorizontalContentAlignment="Center" Padding="0,150,0,0" VerticalContentAlignment="Bottom">
<Label.Effect>
<DropShadowEffect BlurRadius="9" ShadowDepth="0" Color="Aqua"/>
</Label.Effect>
</Label>
</Grid>
private void mySlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
this.textBlock1.Text = Math.Round(this.mySlider.Value/100,2).ToString();
}
The label is covering it up so the touch doesn't get to it. Set IsHitTestVisible=false on the label and anything else you don't want to receive touch input. Mouse input works the same way. Tools like "Snoop" are great for debugging issues like this
I dont know what I have done wrong but whenever my service model is returning less than 2 results I am getting an error.
I have asked on the silverlight forum with no joy.
System.ServiceModel.DomainServices.Client.DomainOperationException: Load operation failed for query 'GetEmployees'. Unexpected end of file. Following elements are not closed: LastModifiedDate, Employee, RootResults, GetEmployeesResult, GetEmployeesResponse. ---> System.Xml.XmlException: Unexpected end of file. Following elements are not closed: LastModifiedDate, Employee, RootResults, GetEmployeesResult, GetEmployeesResponse.
at System.ServiceModel.DomainServices.Client.WebDomainClient`1.EndQueryCore(IAsyncResult asyncResult)
at System.ServiceModel.DomainServices.Client.DomainClient.EndQuery(IAsyncResult asyncResult)
at System.ServiceModel.DomainServices.Client.DomainContext.CompleteLoad(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
My domain service is basic and looks like this
[Query(IsDefault = true)]
public IQueryable<Employee> GetEmployees()
{
return this.ObjectContext.Employees.Where(e=>e.IsDeleted==false);
}
My xaml looks like this
<riaControls:DomainDataSource
AutoLoad="True"
x:Name="dsEmployee"
LoadedData="dsEmployee_LoadedData"
SubmittedChanges="dsEmployee_SubmittedChanges"
QueryName="GetEmployees">
<riaControls:DomainDataSource.DomainContext>
<my:MyDomainContext />
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
<ListBox
x:Name="lstEmployees"
ItemsSource="{Binding ElementName=dsEmployee, Path=Data}"
Grid.Row="1"
VerticalAlignment="Top"
Height="360" MouseLeftButtonUp="lstEmployees_MouseLeftButtonUp">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal"
x:Name="stackEmployees"
MouseRightButtonDown="lstEmployees_MouseRightButtonDown">
<TextBlock Text="{Binding EmployeeName}"
Style="{StaticResource RewardGridText}"
Width="220"
TextAlignment="Left">
<telerik:RadContextMenu.ContextMenu>
<telerik:RadContextMenu x:Name="ContextMenu"
ItemClick="ContextMenuClick"
Opened="ContextMenuOpened">
<telerik:RadMenuItem Header="Edit" />
<telerik:RadMenuItem Header="Disable" />
<telerik:RadMenuItem Header="Delete" />
</telerik:RadContextMenu>
</telerik:RadContextMenu.ContextMenu>
</TextBlock>
<TextBlock Text="{Binding Converter={StaticResource RoleConverter}}" Style="{StaticResource RewardGridText}" Width="160" TextAlignment="Left"/>
<TextBlock Text="{Binding Converter={StaticResource AccountStatusConverter}}" Style="{StaticResource RewardGridText}" Width="160" TextAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
My codebind is just handling the errors. Does anyone know what I have wired up wrong?
I ran into the same issue when using the Telerik RadCompression and RadUploadModule HttpModules. I removed the two lines below from my web.config, and everything started working again.
<add name="RadCompression" type="Telerik.Web.UI.RadCompression" />
<add name="RadUploadModule" type="Telerik.Web.UI.RadUploadHttpModule, Telerik.Web.UI" />