|
The actual service contract is defined as an interface like this:
[ServiceContract]
public interface IMyInterface
{
[OperationContract]
List<int> DoSomething(MyData item);
} This contract will be implemented by a concrete class, and DoSomething will be visible to systems that consume the service. The DataContract is defined as a concrete class like this:
[DataContract]
public class MyData
{
[DataMember]
public int DataValue1 { get; set; }
[DataMember]
public string Identifier { get; set; }
} Then we define the class that implements the service contract like this:
public class ImplementInterface : IMyInterface
{
public List<int> DoSomething(MyData item)
{
return item.DataValue1;
}
}
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Excuse me I miss something:
[ServiceContract]
public interface X
{
[DataMember]
List<Y> Data {get;}
}
[Data Contract]
public class Y
{
[DataMember]
public List<object> Data
{
get;
set;
}
}
Having read your code I does not see why mine is incorrect
|
|
|
|
|
Is class Y supposed to inherit interface X?
If so, you haven't shown that, and why do you have
List<Y> in interface X and List<object> in class Y?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
The class Y doesn't inherit from the interface X, May be the confusion come because I gived the same identifier "data" for two different things!
I create the class Y just to define a new type (structured type) wich is not just composed from List<object> but other DataMembers such as string, int and a new other type Z, wich I create exactly in the same way as Y
and in my interface X, I have a DataMember (a property) wich type is List<y>.
Perhaps I not very Bright to express what I mean, therefore I detail the code of class Y and class Z down:
[DataContract]
public class Y
{
[DataMember]
public int id {get; set;}
[DataMember]
public string Str {get; set;}
[DataMember]
public Z NewType {get; set;}
[DataMember]
Public List<object> Data {get; set;}
}
[DataContract]
public class Z
{
[DataMember]
public int idz {get; set;}
[DataMember]
public string Strz {get; set;}
}
Thank you in advance Mark Salsbery !
|
|
|
|
|
You've create a ServiceContract here, but you are using DataMembers. DataMembers belong to DataContracts - not ServiceContracts. In a ServiceContract, you use OperationContract to mark methods that will be consumed. Take another look at the code I gave you, and pay particular attention to which attribute is defined against which type.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
But here it is not a method but a property in my ServiceContract, if I mark it with [operationContract] I have this error:
<big>Attribute 'OperationContract' is not valid on this declaration type. It is only valid on 'method' declarations.</big>
Is it impossible to declare a property in a ServiceContract ?
|
|
|
|
|
It's not impossible - you just have to do it slightly differently. What you do, is declare OperationContract on your get and set, so you get:
public int MyProperty
{
[OperationContract]get;
[OperationContract]set;
}
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
thank you pete O'Hanlon , It is what I was looking for and thank you again
|
|
|
|
|
You're welcome.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Hi All,
I've just noticed something in the Juval Lowy book I'm reading at the mo. He hasn't actually mentioned this in the text but I've noticed that all his DataContract are structs not classes.
Kinda makes sense really, are you WCF'ers out there tending to do the same with your DCs?
Cheers,
|
|
|
|
|
Nope, and here are my reasons:
- If your contract isn't small, a struct doesn't make much sense. If we assume you have non-trivial data, then the stack usage more than outweighs the prevention of heap allocation that structs bring to the table.
- You cannot use default constructors or field initializers with structs, which we use a fair bit.
- Structs tend to make sense when they are used in lots of places. Assuming that they don't form a large part of your architecture, then you aren't going to see much benefit from them.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Hi Pete,
I was just thinking about in terms of the internal server farm project I'm working on. I've designed a set of base data classes for our database team to use in relation to reading data from the DB and then shoving it down the pipe to a WCF service. They are small data classes.
Might need to do some testing around this to help weigh up the pros and cons.
Cheers,
|
|
|
|
|
I have a form that has several labels that are updated on a regular basis. I was under the assumption that if I set up a proper class (inheriting from INotifyPropertyChanged), events would be sent to the form whenever a property changed. So, I wrote this (abbreviated to avoid boring you):
public class FormProperties : INotifyPropertyChanged
{
private string m_scrambledWord;
public string ScrambledWord
{
get { return m_scrambledWord; }
set
{
m_scrambledWord = value;
OnPropertyChanged("ScrambledWord");
}
}
public FormProperties()
{
m_scrambledWord = "test";
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
And in the form, I'm doing this:
FormProperties m_formProperties = new FormProperties();
Finally, I'm doing this in the XAML:
<Window x:Class="AnagramsWPF.WindowMain"
x:Name="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:AnagramsWPF"
Title="Anagrams/WPF"
Height="566" Width="487"
FontFamily="Arial" FontSize="13"
DataContext="m_formProperties"> <----------------
<Grid>
<TextBox Name="textboxScrambled"
Height="23"
Width="126"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="111,6,0,0"
IsReadOnly="True"
MaxLines="1"
MaxLength="10"
CharacterCasing="Upper"
Text="{Binding Path=ScrambledWord, Mode=OneWay}" />
</Grid>
</Window>
I was expecting the form to be initialized with the associated textbox being set to "test". What am I doing wrong?
BTW, this databinding stuff seems like it's the long way around to setting the controls on a form.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
John - your problem is in the constructor:
m_scrambledWord = "test";
As you are setting the member directly, the property isn't invoked - hence, the notification mechanism doesn't fire. Change this to:
ScrambledWord = "test";
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Still no workee. :/
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
You cannot bind to a field. You would have to expose it as a CLR property.
|
|
|
|
|
I really should read these posts more carefully. What you need to do is set your binding up explicitly, as in:
<Window x:Class="JsopBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" x:Name="myWindow"> <-- Set the name here - you need it below -->
<Window.DataContext>
<Binding ElementName="myWindow" Path="Properties" /> <-- Now, set up the data context binding -->
</Window.DataContext>
<Grid>
<TextBox
Name="textboxScrambled"
Height="23"
Width="126"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="111,6,0,0"
IsReadOnly="True"
MaxLines="1"
MaxLength="10"
CharacterCasing="Upper"
Text="{Binding Path=ScrambledWord, Mode=OneWay}" />
</Grid>
</Window> And, in your code behind you add the property:
public FormProperties Properties
{
get { return m_formProperties; }
set { m_formProperties = value; }
} Alternatively, you can assign your datacontext directly in the code behind as
this.DataContext = m_formProperties;
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Yeah, that worked (setting up a property that gets/sets the ForumProperties).
Many thanks, oh great and powerful Oz.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
You are soooo welcome, now let's go and trample on some egos.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Pete O'Hanlon wrote: now let's go and trample on some egos.
I do that at every opportunity. However, I believe in fair play, and therefore feel that I must wait for someone to actually step on their dick before I point and laugh.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
I prefer to be pre-emptive. When I passed 40, I realised that I enjoyed being a grumpy old sod, so I didn't have to worry about any touchy feely crap.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I find that waiting for some idiot to provide me with a basis for my comments allows me to better organize my thoughts.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Hi ,
I need to develop a Custom List control in WPF in which i need to customize the list header,the list selection,list background and need to add images to the listitem. I have created like this one in MFC with ownerdraw but not aware of WPF. Can anyone let me know some articles helping me to develop one.
Thanks
|
|
|
|
|
Why do you need to create a custom list control? The whole point behind the controls in WPF is that they are "lookless". In other words, their behaviour is separated from the visuals, so you can restyle existing controls with relative ease.
Here's a (pretty completely) restyled version of a ListBox (thanks to KaXAML):
<Style x:Key="{x:Static GridView.GridViewScrollViewerStyleKey}" TargetType="{x:Type ScrollViewer}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid Background="{TemplateBinding Background}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DockPanel Margin="{TemplateBinding Padding}">
<ScrollViewer
DockPanel.Dock="Top"
Focusable="false"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Hidden">
<GridViewHeaderRowPresenter
Margin="2,0,2,0"
AllowsColumnReorder="{Binding Path=TemplatedParent.View.AllowsColumnReorder,RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderContainerStyle="{Binding Path=TemplatedParent.View.ColumnHeaderContainerStyle,RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderContextMenu="{Binding Path=TemplatedParent.View.ColumnHeaderContextMenu,RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderTemplate="{Binding Path=TemplatedParent.View.ColumnHeaderTemplate,RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderTemplateSelector="{Binding Path=TemplatedParent.View.ColumnHeaderTemplateSelector,RelativeSource={RelativeSource TemplatedParent}}"
ColumnHeaderToolTip="{Binding Path=TemplatedParent.View.ColumnHeaderToolTip,RelativeSource={RelativeSource TemplatedParent}}"
Columns="{Binding Path=TemplatedParent.View.Columns,RelativeSource={RelativeSource TemplatedParent}}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
<ScrollContentPresenter Name="PART_ScrollContentPresenter" KeyboardNavigation.DirectionalNavigation="Local"/>
</DockPanel>
<ScrollBar
Name="PART_HorizontalScrollBar"
Grid.Row="1"
Maximum="{TemplateBinding ScrollableWidth}"
Orientation="Horizontal"
Value="{TemplateBinding HorizontalOffset}"
ViewportSize="{TemplateBinding ViewportWidth}"
Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"/>
<ScrollBar
Name="PART_VerticalScrollBar"
Grid.Column="1"
Maximum="{TemplateBinding ScrollableHeight}"
Value="{TemplateBinding VerticalOffset}"
ViewportSize="{TemplateBinding ViewportHeight}"
Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="GridViewColumnHeaderGripper" TargetType="{x:Type Thumb}">
<Setter Property="Width" Value="18"/>
<Setter Property="Background" Value="#404040"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Border Background="Transparent" Padding="{TemplateBinding Padding}">
<Rectangle Width="1" HorizontalAlignment="Center" Fill="{TemplateBinding Background}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="{x:Type GridViewColumnHeader}" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
<Grid>
<Border
Name="HeaderBorder"
Background="#E0E0E0"
BorderBrush="#404040"
BorderThickness="0,1,0,1"
Padding="2,0,2,0">
<ContentPresenter
Name="HeaderContent"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="0,0,0,1"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<Thumb
x:Name="PART_HeaderGripper"
HorizontalAlignment="Right"
Margin="0,0,-9,0"
Style="{StaticResource GridViewColumnHeaderGripper}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="HeaderBorder" Property="Background" Value="#C0C0C0"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="HeaderBorder" Property="Background" Value="#E0E0E0"/>
<Setter TargetName="HeaderContent" Property="Margin" Value="1,1,0,0"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Role" Value="Floating">
<Setter Property="Opacity" Value="0.7"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
<Canvas Name="PART_FloatingHeaderCanvas">
<Rectangle Width="{TemplateBinding ActualWidth}" Height="{TemplateBinding ActualHeight}" Fill="#60000000"/>
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="Role" Value="Padding">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
<Border
Name="HeaderBorder"
Background="#E0E0E0"
BorderBrush="#404040"
BorderThickness="0,1,0,1"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="{x:Type ListView}" TargetType="{x:Type ListView}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListView}">
<Border
Name="Border"
Background="#FFFFFF"
BorderBrush="#888888"
BorderThickness="1">
<ScrollViewer Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}">
<ItemsPresenter/>
</ScrollViewer>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="Background" Value="#AAAAAA"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="{x:Type ListViewItem}" TargetType="{x:Type ListViewItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border
Name="Border"
Background="Transparent"
Padding="2"
SnapsToDevicePixels="true">
<GridViewRowPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="#DDDDDD"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#888888"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I have a WPF project containing a RichTextBox .
I want the text not to wrap. I can force this by setting the PageWidth for the contained FlowDocument to be a large number. However, I really want the PageWidth to adjust itself to the length of the longest line (paragraph), so that the horizontal scroll bar thumb will have an appropriate length.
I tried setting PageWidth to Auto , but this did not prevent wrapping.
Thanks for your suggestions!
tfhain
|
|
|
|
|