I am fairly new to C#. This may be a fairly straight forward question, but I couldn't find any example about this. I am just wondering if there is any way when I create XAML element in the code-behind file(.xaml.cs), I can use other open source library(such as GongSolutions.WPF.DragDrop) property in the element?
An example will be as following, can I create the following XAML code in the code-behind(.xaml.cs) file?
<...
xmlns:dd="urn:gong-wpf-dragdrop"
...
>
...
<ListBox ItemsSource="{Binding Collection}"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True" />
Thanks for your help!!!
You generally need to know how to set values for AttachedProperties from code behind. Below code should help for your case,
listBox.SetValue(GongSolutions.Wpf.DragDrop.DragDrop.IsDragSourceProperty, true);
listBox.SetValue(GongSolutions.Wpf.DragDrop.DragDrop.IsDropTargetProperty, true);
If you want to be type safe, you can set the same like this,
GongSolutions.Wpf.DragDrop.DragDrop.SetIsDragSource(listBox, true);
GongSolutions.Wpf.DragDrop.DragDrop.SetIsDropTarget(listBox, true);
Related
I am new to XAML and C#
I have an icon created already in a project and and I have to use this icon whenever I select one of the option from the dropdown menu.
I made a stackpanel in XAML file
<StackPanel Name="stackPanelforIcon">
</StackPanel>
In the code behind file I have different cases for the dropdown menu.
case IconOnSelect:
?????? = IconList.NewIcon;
This NewIcon is the one already created and I am using the source also for this
using IconProject.Iconlists;
On writing IconList.NewIcon I am not getting any error, it is referenced correctly.
What should I write at ?????? to reference it. Is there any other way apart from using stackPanel to include an icon
A StackPanel cannot show an icon on it's own. You need a control for it, for example an Image.
<StackPanel Name="stackPanelforIcon">
<Image x:Name=theImage" />
</StackPanel>
Then you can use your Icon in your code behind like this:
this.theImage.Source = IconList.NewIcon;
You may need to convert your value, you never said what type it actually is.
Please note that using code-behind is not the preferred way with WPF. Using MVVM is way easier and more natural working with WPF, using code-behind you will fight WPF all the way. Using MVVM, this could be:
<StackPanel Name="stackPanelforIcon">
<Image Source="{Binding CurrentImage}" />
</StackPanel>
with your ViewModel having a property called CurrentImage that you would set when you want to change it. Don't forget to implement INotifyPropertyChanged for the changes to take effect though.
I have a Longlistselector that is getting the text through data binding. My problem is I need it to be localized so it displays the right language, how is the right way to do it?
I tried the first way that came to my head, but I thought it doesn't work:
MainPage.cs
_UserAdBL.Add(new UserAdB("{Binding LocalizedResources.UsText01, Source={StaticResource LocalizedStrings}}"));
Any help will be appreciated, thanks!
If I understand you correctly, you want to get the value of the {Binding LocalizedResources.UsText01, Source={StaticResource LocalizedStrings}} XAML binding in C#.
So use AppResources.UsText01 like so
_UserAdBL.Add(new UserAdB(AppResources.UsText01));
How to bind xml to wpf treeview? I am using Prism mvvm pattern. I will prefer an IList for holding the data for looping.
I have tried http://geeklyeverafter.blogspot.com/2010/03/wpf-treeview-bound-to-xml-file.html and
http://www.blogs.intuidev.com/post/2009/12/28/xml_to_treeview.aspx
but nothing worked.
OK. It is The question is quite old now, but I think there is a simple way to bind XML to a TreeView. Maybe it is helpful for someone.
XAML:
<Window.Resources>
<HierarchicalDataTemplate ItemsSource="{Binding Path=Elements}" x:Key="NodeTemplate">
<Grid>
<TextBlock Text="{Binding Path=Name}"/>
</Grid>
</HierarchicalDataTemplate>
</Window.Resources>
...
<TreeView x:Name="myTreeView" Grid.Column="0"
ItemsSource="{Binding Path=Root.Elements}"
ItemTemplate="{StaticResource ResourceKey=NodeTemplate}"
/>
In the code behind I just create a XDocument (System.Xml.linq) and bind this one to the DataContext of the TreeView. For example like this:
private XDocument _theXML;
public XDocument TheXML {
get => _theXML;
set => _theXML = value;
}
public MainWindow()
{
...
InitializeComponent();
DataContext = this;
TheXML = XDocument.Load(#"c:\file.xml");
myTreeView.DataContext = TheXML;
myTreeView.UpdateLayout();
}
That's it. The content of the XML file will be shown as a TreeView. If you like to see some more Details (Attributes, ...) you can refine the Template in the XAML code.
The way I've done it is to create a method that builds the tree view to a treeview property. Set the WPF treeview items binding to the items property of the treeview property in your class. Of course implementing INotifyPropertyChanged in your ViewModelBase is essential.
I would be happy to give an example, but I do not have access to the internet on my PC at the moment
I did see in the post and do agree that this is not the most proficient way . However since you are already using xml serialization, the parsing of xml is done, now you just have to use the data.
I think that if you were not going to serialize, then the links you posted would hold more validity in the methodology you are you are trying to achieve. But that is just IMO. I will update with some working code when I get the chance tomorrow, the idea of data binding directly to xml sounds fun.
In the meantime check this link out. It looks pretty strait forward.
http://social.msdn.microsoft.com/Forums/vstudio/en-US/cbdb2420-1403-436f-aa7f-b1e3b1acb398/binding-any-xml-document-to-wpf-treeview?forum=wpf
Wondering if there's any trick to accessing a nested control in the code-behind ?
given some XAML along the lines of
<UserControl>
<textbox />
<DataGrid Name="MyGrid">
<Columns>
<Column field=ABC>
<EditType>
<ComboBox Name="myCombo1"/>
I can access this.MyGrid but cannot access this.myCombo1 !!
Everytime that I have run into this I have be able to resolve it by using x:Name instead of Name. This may not be the issue in your case but give it a try.
In simple cases you can access elements using their name, but looks like combobox is datatemplate part, you can work with visual tree at runtime using VisualTreeHelper, but if you use mvvm & code right usually you don't need to access elements directly. Can you provide more information on general problem or test project to reproduce issue?
I have a databound Silverlight DataGrid control that I am trying to sort. I am using RIA services (beta) for my data source, if that makes any difference.
I am quite new to databinding in Silverlight, so this might be something really obvious that I've missed, but I can't seem to find any info on it. I want to be able to set the binding of the ItemSource to a collection in xaml using binding syntax, and have it sorted on one column.
I realize I could set the ItemsSource in code and use LINQ to .OrderBy(). But I don't get a binding that way. It seems like there should be a simple way to do this but I can't find one. How can I keep the binding yet order my collection?
have a look at using a CollectionViewSource. You basically use one as a 'middleman' between your actual collection of data and you data-bound control.
rough example:
<Window.Resources>
<CollectionViewSource
Source="{Binding <<<bind to your collection here >>> }"
x:Key="myDataView" />
</Window.Resources>
...
<ListBox Name="lsyFoo"
ItemsSource="{Binding Source={StaticResource myDataView}}">
...
then in your code behind:
myDataView.SortDescriptions.Add(
new SortDescription("<<<insert property to sort by>>>", ListSortDirection.Ascending));
(ps. you can also add grouping using PropertyGroupDescription)
As you are using RIA Services, you can use the DomainDataSource in your XAML. This will allow you to add SortDescriptors which will do your ordering. See my example below:
<riaControls:DomainDataSource.SortDescriptors>
<riaData:SortDescriptor Direction="Ascending"
PropertyPath="Name" />
</riaControls:DomainDataSource.SortDescriptors>