.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
Easy Web
Imran Ghani
Post New Web Links

Binding an ObservableCollection via XAML without code behind

Posted By:      Posted Date: October 05, 2010    Points: 0   Category :WPF
 

 

I think I am missing something basic here.. But could someone show me what I am doing incorrectly?

MainWindow.xaml.cs

  public partial class MainWindow : Window
  {
    public ObservableCollection<string> TestOC = new ObservableCollection<string> { "First" };

    public MainWindow()
    {
      InitializeComponent();
      TestOC.Add("Second");     
    }

 the XAML file:

<Window x:Class="WpfApplication2.MainWindow"
    ...">
  <Grid>
    <ListView ItemsSource="{Binding TestOC}"/>
  </Grid>
</Window>

Shouldn't that just work?

 


View Complete Post


More Related Resource Links

Binding RadioButtonList from Code Behind

  
I'm trying to populate my form that is made up of RadioButtonLists, a Text Box and a DropDown via Code Behind.  WHen I run it, the Text Box and the DropDown populate, but the RadioButtonLists do not.  I'm guessing it has to do with the SelectedValue command, but I'm not sure what to use.  Here is my aspx page:<ContentTemplate> <div style="height:402px;"> <br /> <table> <tr> <td colspan="2"> <p style="font-size:90%;">Here, you can set your preferences for notifications during the draft. You can change them at any time.</p> <p style="font-size:90%;">You will always be emailed if it is your turn to pick and you have no players in the Pick Queue.</p> </td> </tr> <th colspan="2"> Email Options </th> <tr> <td> Send

How to set Panel.Zindex value though XAML code if any of the Grid/UserControl children having the fo

  
Hi, I have a Grid with 10 buttons. If any of these buttons has focus then I want to set the Panel.Zindex = 999 for the Grid. How to do this in XAML code?Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This will help other members to find the solution easily.

Binding a property within a style - in code

  
So if I had a style as follows <Style TargetType={x:Type Button}> <Setter Property="Background" Value={Binding myproperty} /> </Style> How could I achieve this in code? So far I got Style x = new Style(); Setter a = new Setter(TextBox.Property, new Binding("myproperty")l but there seems to be something wrong with the way I set up the binding, how can I correct it? Thanks

Data binding without typing out xaml

  
Now I'm not stumped on getting data binding to work, I simply want to know how to utilize the window interfaces that visual studio 2010 comes with - as opposed to typing in XAML.  All I want to do is bind a font size property to an xml file within the project.   Here's what I do: After selecting the text box, in the properties panel, i go to the text category then click on the data-binding icon next to the font size field.  I then click on 'apply data binding', where a small panel pops up.  This is where I become stuck.  It seems like I need a path to the xml file, but I don't know how to add that in without typing in XAML.  I would assume that I would be able to add in a resource somewhere - kind of like windows forms allow me to do. I've never worked with WPF before, so I know it's something small/simple that i'm missing.  Any help would be appreciated. thanks, Phil

Data binding without typing out xaml

  
Now I'm not stumped on getting data binding to work, I simply want to know how to utilize the window interfaces that visual studio 2010 comes with - as opposed to typing in XAML.  All I want to do is bind a font size property to an xml file within the project.   Here's what I do: After selecting the text box, in the properties panel, i go to the text category then click on the data-binding icon next to the font size field.  I then click on 'apply data binding', where a small panel pops up.  This is where I become stuck.  It seems like I need a path to the xml file, but I don't know how to add that in without typing in XAML.  I would assume that I would be able to add in a resource somewhere - kind of like windows forms allow me to do. I've never worked with WPF before, so I know it's something small/simple that i'm missing.  Any help would be appreciated. thanks, Phil

How to set MaxLength on DataGridTextColumn in C# code, not XAML

  
Hi. Im creating DataGridTextColumns dynamicly in C# but I need to limit the column input length. Is there a way to do it? I tried to use textbox style because textbox has MaxLength property but DataGridTextColumn does not allow to use styles with textbox targettype :( Any ideas please?  

Binding a custom type in XAML ?

  
Hi, I'd like to bind a property "myProperty" of type "MyType" in the text property of TextBlock : <TextBlock Text="{Binding myProperty, Mode=OneWay}" And I overrided the ctype operator to have conversion from "MyType" to String: Public Overloads Shared Narrowing Operator CType(ByVal v As MyType) As String ...................... End Operator But it seems not work! I get the ToString of "MyType" in result but not the return of CType operator. Is it normal ? Why ?   Thanks for your help.  

Input parameters from Workflow No-Code Or Xaml Only

  
I have many activities and a customized workflow Only Xaml and I can not pass a value to a property of a custom activity, following the example of Workflow1.xoml   <pre lang="x-xml"><SequentialWorkflowActivity x:Name="Workflow1" xmlns:ns0="clr-namespace:CustomActivities;Assembly=CustomActivities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow"> <ns0:ReadLineActivity x:Name="readLineActivity1" InputText="" /> <ns0:WriteLineActivity x:Name="writeLineActivity1" OutputText="{ActivityBind readLineActivity1,Path=InputText}" /> </SequentialWorkflowActivity> based on this workflow I pass a value for the property InputText activity readLineActivity1   Thanks a lot!

Dynamically Assign Datagrid Column Binding from XAML

  

I am trying to build a generic user control that will serve as a quick list maintenance screen.

I have a base ViewModel that has two string properties IDColumnName and DisplayColumnName and then a collection of objects to be edited.  Inherited view models will be created for each object type that needs to be maintained.  The usercontrols datacontext is set to the viewmodel.

What I want to do is have the datagrid, who's itemssource is bound to the collection of objects in the viewmodel, have one column and have that one column bound to the property specified in the DisplayColumnName property of the viewmodel, and I'd like to do it all in XAML.  I've got the datagrids rowdetailstemplate being dynamically determined based on the object type but am struggling with dynamically setting which property the one column displays.


Textrange.Load not loading Xaml code into a FlowDocument

  
Hi,

I'm trying to convert text with Xaml tags into a FlowDocument.  I've done this before and it worked fine (as far as I can recall - that was about a year ago.)  Now I can't get this to work:

private Span FormatString(String _textToFormat)
{
    FlowDocument _formattedDocument;
    Span _formattedText;

    _myString = "<FlowDocument xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Paragraph>This is some <Run FontWeight=\"bold\">bold</Run> text.</Paragraph></FlowDocument>";

    _formattedDocument = new FlowDocument();
    _formattedText = new Span();

    TextRange textRange = new TextRange(_formattedDocument.ContentStart, _formattedDocument.ContentEnd);

    MemoryStream msDocument = new MemoryStream((new ASCIIEncoding()).GetBytes(_myString));
    
    if (textRange.CanLoad(DataFormats.Xaml))
        textRange.Load(msDocument, DataFormats.Xaml);
    
    msDocument.Close();


// rest of function trimmed - the problem is above

}
Things work fine up to and including the creation of the MemoryStream (inspection with the debugger shows that the bytes are in there.)  "textRange.CanLoad" also resolves to true, but textRange.Load(msDocument, DataFormats.Xaml) doesn't seem to do anything - after that, a look at "textRa

Extending Page class in XAML and code-behind

  

I have several XAML "Page"s for which I would like to pull the facets that are in common together into a Page class extension I am calling ACPage. I must be missing some essential piece of knowledge, although I think I am pretty close. Can someone tell me what I am missing? I am learning C#, XAML, .NET, etc. all simultaneously.

I get the following warning:
Warning 1 'Al_Cr.PageWelcome.InitializeComponent()' hides inherited member 'Al_Cr.ACPage.InitializeComponent()'. Use the new keyword if hiding was intended. D:\Projects\Al Cr\Al Cr\Al Cr\obj\x86\Release\PageWelcome.g.cs 50 21 Al Cr

and this error:
Error 2 'Al_Cr.ACPage' cannot be the root of a XAML file because it was defined using XAML. Line 1 Position 15. D:\Projects\Al Cr\Al Cr\Al Cr\PageWelcome.xaml 1 15 Al Cr

The warning points to the auto-generated PageWelcome.InitializeComponent() function definition. The error points to the end of the bolded text, below.

My ACPage.xaml & ACPage.xaml.cs is defined thusly:

<Page x:Class="Al_Cr.ACPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="

Data Binding RadioButton to enum via code

  

I was reading this solution on how to bind a RadioButton to a enum type, but in my special case I need to do it programmatically since I am generating a number of panels with 3 radiobuttons the number of which is only known runtime.

I tried this: 

Binding b = new Binding("CerradaBind");
        b.Mode = BindingMode.TwoWay;
        b.Source = _exp3Class;
        _gpuertas[i].BotonCerrada.SetBinding(_gpuertas[i].BotonCerrada.IsChecked, b);

But found out the IsChecked property is not what the Setbinding function expects, how should I do it?

Also, is there another way of binding directly to radiogroup instead of button by button?


Binding Question How does one set datacontext to same class in XAML?

  

I have a usercontrol with one button in it.  I want to set the text via a property getter setter using the INotifyPropertyChanged interface in code behind.  I can get it to work if I set the DATACONTEXT in code behind in the CTOR like this : DataContext = this;  Everything works perfect. 

But here's the problem.  If I try to do this in xaml by defining an XMLNS which points to the folder this usercontrol is in, and then attempt to reference it as a resouce like this <local:XXXXXX  <-------The XXXX which is the class name of this control can't be found?  Is that wierd?  Can't a resource be itself?  So that I can set the Datacontext in the XAML instead of code behind?  I've tried to bind to relative source self, but cannot set the property (Event when I attempted to created a dependency property).....  Just wondering how one sets up Datacontext to the same class? 


Javaman

Binding Datatable to listview in Xaml

  

Hi,

  Can any one give me solution for below code.  I know how to bind the datatable to listview in code behind, but  i don't know, how to bind it through XAML. This is my sample code. Please any one help me.

<Window x:Class="ListViewDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:ListViewControl;assembly=ListViewControl"
xmlns:local="clr-namespace:ListViewDemo"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate x:Key="checkBox">
<CheckBox IsChecked="{Binding Path=Check, Mode=TwoWay}"/>
</DataTemplate>
</Window.Resources>
<Grid>
<ListView Name="lvProcess">
<ListView.View>
<GridView>
<GridViewColumn Header="Dep_Type" DisplayMemberBinding="{Binding Path=Dep_Type}"/>
<GridViewColumn Header="Check" DisplayMemberBinding="{Binding Path=Check}"/>
<GridViewColumn Header="Depend

calling a code behind method using xaml?

  

Is it possible to call a method in a XAML's code-behind through XAML itself? ie:

<Image Source="{Binding GetRandomImagePath}" />

where

public string GetRandomImagePath(...){////}

?


How to retrieve the original XAML file name from code

  

Hi,

I would like to display the original filename for the element the mouse is hovering over. Can I retrieve the original XAML-FileName from a FrameworkElement in code?

Regards,
Paul Sinnema


Take a look at my blog: http://paul.sinnema.ch

Step Into Code (net.tcp binding)

  
I have a service that is now currently working 100% with net.tcp.  I used to use the wsHTTP binding.  Now, I have found that for some reason (and I don't know why) I cannot seem to step into the code using the net.tcp binding.  It would be helpful if I could still step into the code and still use the net.tcp binding.  I have confirmed that my IDE has not somehow been switched to Release mode (which seems to ignore breakpoints). 

I can still debug in the client app, but when I try to step into the code via the net.tcp binding it does not work.

The server binding configuration is pasted below.
<bindings>
      <netTcpBinding>
        <binding name="AthenaBindingCfgTCP">
          <security mode="Transport">
            <transport protectionLevel="Sign"/>
            <message clientCredentialType="Windows"/>
          </security>
        </binding>
      </netTcpBinding>
 
Categories: 
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend