I try to find a way to put my Combobox (UserControl) over another grid, but I can't. I'm tried to use Panel.ZIndex, ClipToBonus but they not working.
My XAML Code
<Grid>
<Grid.RowDefinitions>
<RowDefiniton Height="50"/>
<RowDefiniton/>
</Grid.RowDefinitons>
<Grid Background="#1e1e1e" ClipToBonus="false" Panel.ZIndex="10">
<control:Combobox x:Name="CbType" Width="200" Height="30" Panel.ZIndex="10"/>
</Grid>
<Grid Grid.Row="1" Background="#1e1e1e" ClipToBonus="false" Panel.ZIndex="1">
<control:ViewData x:Name="ViewControl" Width="500" Height="300" Panel.ZIndex="1"/>
</Grid>
</Grid>
I want when Combobox(CbType) drop-down it shows over ViewData(ViewControl). What way I can do for my problem?
Thank for your attention.
I'm developing a WPF user control and I want it to display some design-time data. It's proving to be quite a challenge to get things just right and I could really use your help.
Here's my control so far, basically a <ListBox>:
<UserControl x:Class="TA.Weather.UI.Wpf.WorkingSetEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Loaded="HandleLoadedEvent"
>
<Grid d:DataContext="{d:DesignData Source=/SampleData/WorkingSet.xaml}">
<ListBox x:Name="WorkingSetListBox" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="30" />
<ColumnDefinition MinWidth="30" />
<ColumnDefinition MinWidth="30" />
<ColumnDefinition MinWidth="30" />
<ColumnDefinition MinWidth="30" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" TextAlignment="Center"
Text="{Binding Path=[0].Limits.StationId}"></TextBlock>
<TextBlock Grid.Column="1" TextAlignment="Center"
Text="{Binding Path=[0].Limits.SensorId}"></TextBlock>
<TextBlock Grid.Column="2" TextAlignment="Center"
Text="{Binding Path=[0].Limits.SensorName}"></TextBlock>
<TextBlock Grid.Column="3" TextAlignment="Center"
Text="{Binding Path=[0].Limits.LowerLimit}"></TextBlock>
<TextBlock Grid.Column="4" TextAlignment="Center"
Text="{Binding Path=[0].Limits.UpperLimit}"></TextBlock>
<CheckBox Grid.Column="5" x:Name="MonitoredCheckBox"
IsChecked="{Binding Path=[1].Monitored}"/>
</Grid>
</ListBox>
</Grid>
</UserControl>
I figured out how to create the design-time data in another XAML file, and set the build action to DesignDataWithDesignTimeCreatableTypes. The type is essentially just a DTO so it seems OK to create instances at design time. So here's the data file, containing an List<SensorState> with two members:
<generic:List x:TypeArguments="ws:SensorState"
xmlns:ws="clr-namespace:TA.Weather.Core;assembly=TA.Weather.Core"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:generic="clr-namespace:System.Collections.Generic;assembly=mscorlib">
<ws:SensorState IdleTimeout="300" LastUpdate="2015-08-19T03:39:00.000Z" Monitored="False"
WithinAcceptableLimits="False">
<ws:SensorState.Limits>
<ws:SensorLimits StationId="12-34-56-78-90-12" SensorId="1" SensorName="Inside Temperature"
LowerLimit="-10" UpperLimit="30" />
</ws:SensorState.Limits>
</ws:SensorState>
<ws:SensorState IdleTimeout="300" LastUpdate="2015-08-19T03:39:00.000Z" Monitored="True"
WithinAcceptableLimits="False">
<ws:SensorState.Limits>
<ws:SensorLimits StationId="12-34-56-78-90-12" SensorId="1" SensorName="Inside Temperature"
LowerLimit="-10" UpperLimit="30" />
</ws:SensorState.Limits>
</ws:SensorState>
</generic:List>
Thus far, this gets me a user control that looks like this:
So, success of sorts. However, there are two questions at this point:
Why does only one row show up when there are two items in the list? I can control which instance shows up by changing the array index in the binding expression, but how do I get the rows to repeat? I tried removing the array index but that just gives a compile error.
I'm getting errors against the data file of the form "The member XXX is not recognized or is not accessible", one for each property that I'm setting, 36 in total. Even though the build succeeds and the design-time data shows up in the editor. They are errors so I'm sure they are important - I just have no idea what the compiler is trying to tell me. So what do these errors mean and what do I need to do so mitigate them?
There's more. I want to use a DataTemplate to create a nicer look for the data; a bit later on there is going to be a slider control for setting the limits, etc. so here's how I've tried to use a template:
<UserControl x:Class="TA.Weather.UI.Wpf.WorkingSetEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:core="clr-namespace:TA.Weather.Core;assembly=TA.Weather.Core"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Loaded="HandleLoadedEvent"
>
<Grid d:DataContext="{d:DesignData Source=/SampleData/WorkingSet.xaml}">
<Grid.Resources>
<DataTemplate x:Key="WorkingSetTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="30" />
<ColumnDefinition MinWidth="30" />
<ColumnDefinition MinWidth="30" />
<ColumnDefinition MinWidth="30" />
<ColumnDefinition MinWidth="30" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" TextAlignment="Center"
Text="{Binding Path=[0].Limits.StationId}"></TextBlock>
<TextBlock Grid.Column="1" TextAlignment="Center"
Text="{Binding Path=[0].Limits.SensorId}"></TextBlock>
<TextBlock Grid.Column="2" TextAlignment="Center"
Text="{Binding Path=[0].Limits.SensorName}"></TextBlock>
<TextBlock Grid.Column="3" TextAlignment="Center"
Text="{Binding Path=[0].Limits.LowerLimit}"></TextBlock>
<TextBlock Grid.Column="4" TextAlignment="Center"
Text="{Binding Path=[0].Limits.UpperLimit}"></TextBlock>
<CheckBox Grid.Column="5" x:Name="MonitoredCheckBox"
IsChecked="{Binding Path=[1].Monitored}"/>
</Grid>
</DataTemplate>
</Grid.Resources>
<ListBox x:Name="WorkingSetListBox" ItemTemplate="{DynamicResource WorkingSetTemplate}">
</ListBox>
</Grid>
</UserControl>
So basically, I've moved the <Grid> out of the <ListBox> and into the <DataTemplate> resource. As soon as I do this, the design-time data vanishes and I'm left with just a blank control in the designer. So my third question becomes:
Why does the design-time data vanish when I use a <DataTemplate>? What do I need to do differently?
You can probably tell that I have almost no experience with WPF... I must admit I am struggling with binding expressions a little bit, they seem like some sort of arcane magic with little rhyme or reason to them. I think maybe I'm missing an important concept somewhere. I hope you can give me a few wise words to push me in the right direction.
I want to do something like that:
I have a base class VerticalConnectorViewModel and a derived class StairsViewModel.
I use WPF data templates to define a view for those objects.
For example my base class is assigned like this (inside the UserControl.Resources:
<!-- Define a data-template for the 'VerticalConnectorViewModel' class. This generates the UI for each node. -->
<DataTemplate DataType="{x:Type model:VerticalConnectorViewModel}">
<network:VerticalConnectorView></network:VerticalConnectorView>
</DataTemplate>
<!-- Define a data-template for the 'StairsViewModel' class. This generates the UI for each node. -->
<DataTemplate DataType="{x:Type model:StairsViewModel}">
<network:StairsView></network:StairsView>
</DataTemplate>
I have created two different Usercontrols, namely the 'network:VerticalConnectorView' and the 'network:StairsView'. Both look similar, except for some additional elements for the latter.
My VerticalConnectorView.xaml file looks like this (but is not really important here I think):
<UserControl x:Class="NetworkUI.VerticalConnectorView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:network="clr-namespace:NetworkUI"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid IsSharedSizeScope="True">
<StackPanel>
<network:NodeView x:Name="nodeView"></network:NodeView>
<Border Grid.Row="0" BorderBrush="Black" BorderThickness="1" Margin="0" SnapsToDevicePixels="True">
<StackPanel Width="Auto" HorizontalAlignment="Stretch" >
<Grid Width="Auto" Height="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="sharedDescription" Width="Auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<network:EditableTextBlock Grid.Column="0" Text="Type" FontSize="10" IsEditable="False"></network:EditableTextBlock>
<ComboBox Grid.Column="1" HorizontalAlignment="Stretch" SelectedItem="{Binding Type}" ItemsSource="{Binding TypeValues}"></ComboBox>
</Grid>
</StackPanel>
</Border>
<Border Grid.Row="0" BorderBrush="Black" BorderThickness="1" Margin="0" SnapsToDevicePixels="True">
<StackPanel Width="Auto" HorizontalAlignment="Stretch" >
<Grid Width="Auto" Height="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="sharedDescription" Width="Auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<network:EditableTextBlock Grid.Column="0" Text="Placement" FontSize="10" IsEditable="False"></network:EditableTextBlock>
<ComboBox Grid.Column="1" HorizontalAlignment="Stretch" SelectedItem="{Binding Placement}" ItemsSource="{Binding PlacementValues}"></ComboBox>
</Grid>
</StackPanel>
</Border>
</StackPanel>
</Grid>
</UserControl>
There are two ComboBox elements. The first one lets you select 'Random', 'Stairs', 'Elevator or 'Escalator'. Now, what I want to do, is if one selects 'Stairs' I want to change my ViewModel from VerticalConnectionViewModel to StairsViewModel (and let the WPF use the other View).
Since I use a Collection for all my ViewModels I already tried to use the SelectionChanged event for the removal of the old ViewModel and the addition of the new one (instance of a different class), but that didn't work either.
Is this possible or should I rethink my approach? I know I can e.g. use only one class and update the UI for different selected values, but should I really add all possible values to my base-class? E.g. it makes no sense to have a StairsType property if the type of the VerticalConnector is set to 'Elevator', right?
I'm starting to try to create a modern app with C# and XAML. I've already worked with C# but I never touched in a XAML or WPF piece of code, so I've a beginner question..
I'm using a MSFT template Hub App (XAML) but I don't know how can I set the text value on a TextBlock through the C# code if that textblock is inside the datatemplate.
Is there somebody who can help me with this one?
I already googled for it but I can't get any site with that answer/explanation.
This is an example about what I'm trying to do:
XAML:
<DataTemplate >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Image Source="Assets/MediumGray.png" Stretch="Fill" Width="420" Height="280"/>
<TextBlock Style="{StaticResource SubheaderTextBlockStyle}" Grid.Row="1" Margin="0,10,0,0" TextWrapping="Wrap"
x:Uid="Section1Subtitle" Text="{Binding Score}"/>
<TextBlock x:Name="desc" Grid.Row="2" Margin="0,10,0,0"
x:Uid="DescriptionHeader" Text="{Binding Test}"/>
<TextBlock x:Name="texttest" Grid.Row="3"
Text="{Binding Name}"/>
</Grid>
</DataTemplate>
C# code:
public class Class1
{
string name = "This is a test";
public string Name
{
get { return name; }
set { name = value; }
}
}
What am I doing wrong here?
Thanks in advance,
Problem solved.
Thanks a lot for you help.
Here is the code with the changed which allow me to get the variable from C#:
<DataTemplate>
<Grid>
<Grid.DataContext>
<local:Class1/>
</Grid.DataContext>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Image Source="Assets/MediumGray.png" Stretch="Fill" Width="420" Height="280"/>
<TextBlock Style="{StaticResource SubheaderTextBlockStyle}" Grid.Row="1" Margin="0,10,0,0" TextWrapping="Wrap"
x:Uid="Section1Subtitle" Text="{Binding Score}"/>
<TextBlock x:Name="desc" Grid.Row="2" Margin="0,10,0,0"
x:Uid="DescriptionHeader" Text="{Binding Test}"/>
<TextBlock x:Name="texttest" Grid.Row="3"
Text="{Binding Name}"/>
</Grid>
</DataTemplate>
This one will also walks you through many aspects of the WP development, including using devices, live tiles, etc:
http://www.jeffblankenburg.com/2011/10/31/31-days-of-mango/
It's a bit old though.
Usually there is no way to set something inside of a DataTemplate easily in c# code. However, there are some messy ways to do it through binding, converters, and selectors.
XAML provides a simple and powerful way to auto-update data between the
business model and the user interface. This mechanism is called
DataBinding. Everytime when the data of your business model changes,
it automatically reflects the updates to the user interface and vice
versa. This is the preferred method in WPF to bring data to the user
interface.
Databinding can be unidirectional (source -> target or target <-
source), or bidirectional (source <-> target).
try out the link Learn XAML.
In abstraction, a DataTemplate is a Visual representation of a certain piece of Data.
UI elements inside a DataTemplate should reflect the state of such piece of data.
In XAML-based technologies, DataBinding helps in keeping the UI in sync with the data, using really clean and beautiful declarative means, as opposed to traditional procedural programming.
So, say you have a certain piece of data, like:
public class Person
{
public string FirstName {get;set;}
public string LastName {get;set;}
}
Then you may have a DataTemplate which represents that data, like:
<DataTemplate DataType="local:Person">
<StackPanel>
<TextBlock Text="{Binding LastName}"/>
<TextBlock Text="{Binding FirstName}"/>
</StackPanel>
</DataTemplate>
The key is in the {Binding} declarations.
See the above linked MSDN article for more information.
Something like this
was available in windows forms, but i forget what it was called. But I'm just looking for some good borders to outline regions that allows me to name the region.
Sounds like you need a GroupBox. I wrote an article about these but I won't post a link, as I don't like using StackOverflow for promoting web sites. I will post the opening example XAML though so you can see the effect and check if it's what you want.
<Window x:Class="GroupBoxDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="GroupBox Demo"
Width="250"
Height="180">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<GroupBox Header="Mouse Handedness">
<StackPanel>
<RadioButton Content="Left-Handed" Margin="5"/>
<RadioButton Content="Right-Handed" Margin="5" IsChecked="True"/>
</StackPanel>
</GroupBox>
<GroupBox Grid.Row="1" Header="Double Click Speed">
<Slider Margin="5" />
</GroupBox>
</Grid>
</Window>
It looks like: