Well, I'd like to use a class of a namespace in my Mainwindow.xaml
Image
As you see the namespace called WPF.Tools
Now I create a local at the top to the XAML
xmlns:webutil="WPF.Tools"
And in the Grid I have a WebBrowser where I'd like to use a a method of the class of the namespace:
<WebBrowser webutil:WebBrowserBehaviors.BindableSource="{Binding SelectedRSSFeed.Link}" Grid.Column="1" Margin="10,10,10,10" Grid.RowSpan="3" Grid.Row="0"/>
If I hover over it it says: The name "WebBrowserBehaviors" does not exist in the namespace "WPF.Tools".
I also added a Project Reference so I don't think this is the Problem
Your reference seems wrong.
It should be:
xmlns:webutil="clr-namespace:WPF.Tools;assembly=WPF.Tools
Related
I am working on a WPF main window and I am using a list box, and I want the list box to auto-scroll whenever new data is added. I used the ListBoxBehavior class in the chosen answer for this question, and I added the following namespaces for the that class in my code:
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
Also, in my XAML, I added the following:
<ListBox x:Name="IncomingData" FontSize="20" Grid.Column="1" Margin="10,10,10,0" Grid.Row="3" ItemsSource="{Binding SourceCollection}" lb:ListBoxBehavior.ScrollOnNewItem="true"/>
However, I am getting the following three errors in my XAML code regarding that line, and they are as following:
Error XDG0006 The namespace prefix "lb" is not defined.
Error XDG0008 ListBoxBehavior is not supported in a Windows Presentation Foundation (WPF) project.
Error XLS0415 The attachable property 'ScrollOnNewItem' was not found in type 'ListBoxBehavior'.
I tried creating an object of a ListBox type ListBox lb = new ListBox(); in the ListBoxBehavior class, but that didn't change the situation. Also, ScrollOnNewItem already exists in the class, so why is it not identifying it?
Is there a missing step that I should have done?
Any help is much appreciated!
you need to define the lb namespace before using it.
at the top of your xaml file you ought to see xmlns:x="...". notice you are using it with x:Name.
same goes for lb. you need to define xmlns:lb="...". intellisense should help you fill in the "...".
notice xmlns means xml namespace.
that ought to clear up all 3 errors.
I cannot set the Button.Visibility = Visibility.Collapsed on any of my buttons.
Below is the xaml
<Button x:Name="createPageButton" Content="Create Page" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="740,75,0,0" Height="61" Width="175" FontSize="24" FontWeight="Bold" Click="CreatePageButton_Click" />
<Button x:Name="TestButton" Content="Button" HorizontalAlignment="Left" Margin="1699,705,0,0" VerticalAlignment="Top" Visibility="Collapsed"/>
In my code behind I try to set the Visibility property.
TestButton.Visibility = Visibility.Visible;
I get the following error msg.
Error CS0176 Member 'Visibility.Visible' cannot be accessed with an instance reference; qualify it with a type name instead
I cannot reproduce your issue with your code above. It worked well on my side with invoking it for example, inside the click event handle.
private void CreatePageButton_Click(object sender, RoutedEventArgs e)
{
TestButton.Visibility = Visibility.Visible;
}
My UWP app version is target build 15063.
Error CS0176 Member 'Visibility.Visible' cannot be accessed with an instance reference; qualify it with a type name instead
But for this error, it seems like you are trying to access static members with instance syntax which is not allowed. Details please reference this similar thread. If you still have issues please upload a minimal reproduced project.
Updated:
If you are using the WindowsTemplateStudio, by default in the template studio blank app, the page doesn't reference the Windows.UI.Xaml namespace (which Visibility class is belong to), so that thrown this error. Just add this reference it will work well.
using Windows.UI.Xaml;
In my case, the cause was due to a conflict in the imports / using namespaces. It seems that I had two namespaces which have the same naming.
To fix this issue, I had to specify the exact class reference: System.Windows.Visibility
Lets assume my XAML is defined as below with my imported namespace in mycontrols.
<Grid x:Class="LayoutGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:mycontrols="clr-namespace:Predefined.Controls;assembly=UIControls"
>
If i was to use something defined in the namespace Predefined.Controls then i would simply reference it with its alias mycontrols
Example:
<mycontrols:MyCustomButton Name="SubmitButton" />
Now if the namespace Predefined.Controls.CustomTextBoxes also existed, is there a way to use a control inside this namespace without having to add it to the XAML definition at the top?
Something like this??
<mycontrols.CustomTextBoxes:MyCustomTextBox Name="TextBox1" />
No. In XML, the namespace prefix defines the namespace, you can't just tack things onto it. You'll need to add the full CLR namespace as an XML namespace declaration in a parent element:
xmlns:ctb="clr-namespace:Predefined.Controls.CustomTextBoxes;assembly=UIControls"
And use that prefix in when you instantiate that element/control in your XAML:
<ctb:MyCustomTextBox />
I am trying out a simple WPF application. The XAML code is:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfApplication1;assembly=WpfApplication1"
Title="My First WPF Demo" Height="350" Width="525">
<Window.Resources>
<sys:Int32 x:Key="i">10</sys:Int32>
<local:Employee x:Key="emp2"></local:Employee> --> THIS LINE
</Window.Resources>
<StackPanel>
<TextBox x:Name="txtName" FontSize="18" Margin="20"></TextBox>
<Button x:Name="btnClickMe" FontSize="18" Margin="20"
Click="btnClickMe_Click">Click Me</Button>
<TextBlock x:Name="lblName" FontSize="18" Margin="20"></TextBlock>
<Label x:Name="lblEmpInfo" FontSize="18" Margin="20"></Label>
<Label x:Name="lblEmpInfo2" FontSize="18" Margin="20"></Label>
</StackPanel>
</Window>
I have a class Employee that is as follows in the same project:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApplication1
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
}
}
When i try to build my project i get the error:
Error 1 The tag 'Employee' does not exist in XML namespace
'clr-namespace:WpfApplication1;assembly=WpfApplication1'. Line 9
Position 10.
Haven't really checked, but... Have you tried removing the assembly=WpfApplication1 part? Leaving only xmlns:local="clr-namespace:WpfApplication1"
use this way in your case:
xmlns:local="clr-namespace:WpfApplication"
I tested your code (Windows 7, Visual Studio 2012 Update 4) and ran into the same situation. After trying some ideas (some months ago I think had the same issue) the following 'workaround' worked for me:
Remove the 'error line' and the namespace from your MainWindow.xaml (but keep your Employee.cs in the Project)
Build the solution! (I think Visual Studio needs to build your Employee.cs before you can instantiate the object in XAML)
Add your Employee and the namespace without the explicit assembly
I'm looking forward hearing whether it fixed the problem
This is a old question but what worked what this:
I change the tag from
<local:MyClass Name="TagName"></local:MyClass>
To:
<local:MyClass Name="TagName"/>
But the following error was thrown:
Because 'MyClass' is implemented in the same assembly, you must set the x:Name attribute rather than the Name attribute.
So i changed to and worked:
<local:MyClass x:Name="TagName"/>
And then tried and worked too:
<local:MyClass x:Name="TagName"></local:MyClass>
I had a similar issue. Tried to rebuild, clean, restart Visual Studio, rebuild again, but nothing solved the issue. I also removed the assembly=... part, it didn't help.
Then I tried moving the project to my hard disk (it was originally in my documents folder, which is located on a mapped network drive), and suddenly the problem was gone! Don't know why, but could be worth a try if anyone else is still having this problem.
I am a beginner of MVVM. I wanted to bind selected treeview item to a textblock. I found a solution here. So I implemented the same in my project...:
<TextBox Text="{Binding SelectedItem, Converter={StaticResource GetTextFromItemConverter}, ElementName=tvMain, Mode=OneWay}"
Also created the GetTextFromItemConverter class in my viewmodel(same as in the solution).
But in xaml I got error saying
The resource GetTextFromItemConverter could not be resolved.
How to resolve this issue??
I suppose the class GetTextFromItemConverter is defined in some namespace called TestDemo. You first have to create some instance of that class as some resource in XAML. As an example, you can add it as some resource of Window.Resources, we need to import the namespace of that class so that we can create an instance of that class, something like this:
<Window ...
xmlns:local="clr-namespace:TestDemo">
<Window.Resources>
<local:GetTextFromItemConverter x:Key="textConverter"/>
</Window.Resources>
<!-- ... -->
<TextBox Text="{Binding SelectedItem,
Converter={StaticResource textConverter},
ElementName=tvMain, Mode=OneWay}"/>
<!-- ... -->
</Window>
Note about the added part xmlns:local="clr-namespace:TestDemo" is to import the TestDemo namespace and aliased as local prefix. The ... is a placeholder for what you have in your Window (auto-generated by designer).
Check whether the view model that you have mapped is properly referenced, if so its perfect check for the path reference to the converter that you have used.
For example:
xmlns:converter="using your namespace path"-location where the converter is placed in the resource dictionary.