|
Yeah, I did try that too, and it worked fine for my other controls when the Dependency Property was within the Control that I wanted to bind to. Btw, you don't need the x:Type to make it work:
Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=XLabel}"
But as soon as I had a collection of different user controls and I wanted to access them, I had to say ElementName for some reason. I guess it was because it couldn't find the correct property in the first Ancestor type.
|
|
|
|
|
Why would this be a better approach?
This space for rent
|
|
|
|
|
You might set the name of the user control to something different when its an element in your window. Setting the name property in the user control will still work though since it is initialized before the containing window. Using Binding.RelativeSource is a suitable option.
"As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"
|
|
|
|
|
I ended up by setting the x:Name and specify the ElementName as I said, but I would really like to know how to implement the RealativSource in my project.
A simple WPF LineChart control[^]
I couldn't see exactly how to do it.
|
|
|
|
|
My answer was in response to Pete's question.
"As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"
|
|
|
|
|
Ah, posted it in the wrong place. I couldn't make it work properly. It simply wouldn't find the x:Type UserControl. It refused to bind, and I don't know how to fix it.
|
|
|
|
|
Hi,
I have a Datagrid, it is bound with an ObsevableCollection of ClassA which has two Properties Id, Name.
Up to here Datagrid works fine, now I want the selected row's Id value of Datagrid to be populated in the ViewModel.
Eventually this should be passed to another View where it will be used.
Can anybody please help me in achieving this, it is very urgent? Any help is appreciated, may be a link, a code snippet or even a suggestion helps me greatly.
Thaanks in advance.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
In your ViewModel, expose a SelectedItem property (of the same type as the entries in your ObservableCollection ). Bind the SelectedItem property from your grid to this property in the ViewModel. When the selection changes in the grid, the underlying selection changes in the VM.
This space for rent
|
|
|
|
|
Hi All,
I have a WPF application named : TestWPFApplication, In that I have folders View and View Model. I have View class as below
namespace TestWPFApplication
{
public partial class MainWindow : Window
{
TestViewModel _testViewModel;
public MainWindow()
{
InitializeComponent();
_testViewModel = new TestViewModel();
DataContext = _testViewModel;
}
}
}
then I have ViewModel class as below.
namespace TestWPFApplication
{
public class TestViewModel : INotifyPropertyChanged
{
TestData _testData;
public TestSpecificCommand TestSpecificCommand { get; set; }
public TestViewModel()
{
_testData = new TestData();
_personNames = new ObservableCollection<TestData>();
this.TestSpecificCommand = new TestSpecificCommand(this);
}
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value; NotifyPropertyChanged("Name");
}
}
private int _id;
public int Id
{
get { return _id; }
set
{
_id = value; NotifyPropertyChanged("Id");
}
}
}}
When I am trying to reference this ViewModel as static resource to bind properties of ViewModel to View then its giving error as below.
<Window x:Class="TestWPFApplication.MainWindow"
xmlns:testns="clr-namespace:TestWPFApplication;assembly=TestWPFApplication"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
>
<Window.Resources>
<testns:TestViewModel x:Key="_testViewModel" Name="Abdul">
</testns:TestViewModel>
</Window.Resources>
<Grid>
<Label Content="Enter Name" HorizontalAlignment="Left" Margin="58,62,0,0" VerticalAlignment="Top" Name="lblName"/>
<TextBox Text="{Binding Name, Mode=OneWay, Source={StaticResource _testViewModel}}" HorizontalAlignment="Left" Height="22" Margin="160,64,0,0" TextWrapping="Wrap"
Name="txtName" VerticalAlignment="Top" Width="120"/>
<Label Content="Enter Id" HorizontalAlignment="Left" Margin="58,17,0,0" VerticalAlignment="Top" Name="lblId"/>
<TextBox Text="{Binding Name, Mode=OneWay, Source={StaticResource _testViewModel}}" HorizontalAlignment="Left" Height="22" Margin="160,18,0,0" TextWrapping="Wrap"
Name="txtId" VerticalAlignment="Top" Width="120"/>
<!--<Button Content="Save" HorizontalAlignment="Left" Margin="277,142,0,0" VerticalAlignment="Top" Width="74" Name="btnSaveSpecific"
Command="{Binding Path=TestSpecificCommand}"
CommandParameter="{Binding ElementName=txtName, Path=Text}" />-->
<Button Content="Save" HorizontalAlignment="Left" Margin="277,142,0,0" VerticalAlignment="Top" Width="74" Name="btnSaveSpecific"
Command="{Binding Path=ClickCommand}"
/>
<DataGrid HorizontalAlignment="Left" Margin="103,188,0,0" VerticalAlignment="Top" Height="98" Width="313"
IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=PersonNames, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name of Employee" Binding="{Binding Path=Name}" />
</DataGrid.Columns>
</DataGrid>
<pre>
</Grid>
And error is as below
Error 1 The tag 'TestViewModel' does not exist in XML namespace 'clr-namespace:TestWPFApplication;assembly=TestWPFApplication'. Line 8 Position 10. \\VBOXSVR\VMShared\Test Projects\TestWPFApplication\TestWPFApplication\View\MainWindow.xaml 8 10 TestWPFApplication
Error 2 The name "TestViewModel" does not exist in the namespace "clr-namespace:TestWPFApplication;assembly=TestWPFApplication". \\VBOXSVR\VMShared\Test Projects\TestWPFApplication\TestWPFApplication\View\MainWindow.xaml 8 9 TestWPFApplication
Error 3 The type 'testns:TestViewModel' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. \\VBOXSVR\VMShared\Test Projects\TestWPFApplication\TestWPFApplication\View\MainWindow.xaml 8 10 TestWPFApplication
<pre>
Any help please? I am not able to understand this, I have ViewModel in the same assembly as View and then I try to reference them using the same Assembly name and Name space name it says the ViewModel class is not available.
Any help is appreciated please help me.
<div class="signature">Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."</div>
|
|
|
|
|
It looks like your view and view-model are in the same assembly. Try removing the assembly from the XML namespace declaration:
<Window x:Class="TestWPFApplication.MainWindow"
xmlns:testns="clr-namespace:TestWPFApplication"
...
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Rick,
I have removed it as below, it is compiling and running executing, but showing me that Invalid Markup on the UI screen, when I try to open the Properties settings, the Properties box is not showing me any Properties, why could that be?
Thank you very much my friend.
<Window x:Class="TestWPFApplication.MainWindow"<br />
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:testns="clr-namespace:TestWPFApplication"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
>
<Window.Resources><br />
<testns:TestViewModel x:Key="_testViewModel" Name="Abdul">
</testns:TestViewModel>
</Window.Resources>
<Grid>
<Label Content="Enter Name" HorizontalAlignment="Left" Margin="58,62,0,0" VerticalAlignment="Top" Name="lblName"/>
<TextBox Text="{Binding Name, Mode=OneWay}" HorizontalAlignment="Left" Height="22" Margin="160,64,0,0" TextWrapping="Wrap"
Name="txtName" VerticalAlignment="Top" Width="120"/>
<pre>
<Button Content="Save" HorizontalAlignment="Left" Margin="277,142,0,0" VerticalAlignment="Top" Width="74" Name="btnSaveSpecific"
Command="{Binding Path=TestSpecificCommand}"
CommandParameter="{Binding ElementName=txtName, Path=Text}" />
<Label Content="Enter Id" HorizontalAlignment="Left" Margin="58,17,0,0" VerticalAlignment="Top" Name="lblId"/>
<TextBox Text="{Binding Name, Mode=OneWay}" HorizontalAlignment="Left" Height="22" Margin="160,18,0,0" TextWrapping="Wrap"
Name="txtId" VerticalAlignment="Top" Width="120"/>
<Button Content="Save 2" HorizontalAlignment="Left" Margin="412,142,0,0" VerticalAlignment="Top" Width="74" Name="btnSaveSpecific2"
Command="{Binding Path=ClickCommand}"
/>
<DataGrid HorizontalAlignment="Left" Margin="103,188,0,0" VerticalAlignment="Top" Height="98" Width="313"
IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=PersonNames, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name of Employee" Binding="{Binding Path=Name}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
modified 16-May-16 12:34pm.
|
|
|
|
|
indian143 wrote: Hi Pete,
I am not Pete.
indian143 wrote: Invalid Markup on the UI screen
Does it give you an error message? Maybe something in the "Error List" or "Output" windows?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Sorry and thank you for bringing it up, good catch
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
Hi Rick,
It says that the
The name "TestViewModel" does not exist in the namespace "clr-namespace:TestWPFApplication.ViewModel". \VBOXSVR\VMShared\Test Projects\TestWPFApplication\TestWPFApplication\View\MainWindow.xaml 9 9 TestWPFApplication
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
But according to the code you posted earlier, it's not in the TestWPFApplication.ViewModel namespace; it's in the TestWPFApplication namespace.
It sounds like you've put:
xmlns:testns="clr-namespace:TestWPFApplication.ViewModel"
instead of:
xmlns:testns="clr-namespace:TestWPFApplication"
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Friends,
I have two Textboxes, One for Id and another for the Name, I need to pass both of these two into MyViewModel class Id and Name Properties in a Button Click, so far I am able to pass one of them to the ViewModel, how can I pass both the parameters to the ViewModel, any help is greatly helpful. Thanks in advance please.
So far I am able to do as below
in View
<Button Content="Save" HorizontalAlignment="Left" Margin="277,142,0,0" VerticalAlignment="Top" Width="74" Name="btnSaveSpecific"
Command="{Binding Path=TestSpecificCommand}"
CommandParameter="{Binding ElementName=txtName, Path=Text}" />
In the Command class
public void Execute(object parameter)
{
var values = (string)parameter;
this.ViewModel.AddAnItem(values);
}
In View Model
public void AddAnItem(string _name)
{
int t;
if ((_personNames == null) || (_personNames.Count() <= 0))
t = 1;
else
t = (_personNames.Count <= 0) ? 1 : _personNames.Max(x => x.Id) + 1;
_personNames.Add(new TestData(t, _name));
}
I want to change this AddAnItem to take two parameters int and string and if possible return value also, but return is not important, what is important is to take two parameters. Please need help, any sort of help a link or code snippet or even a suggestion also helps.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
modified 15-May-16 7:38am.
|
|
|
|
|
Why? Just put two string properties in your ViewModel and binding your Text boxes to them. You can then read them from your command.
This space for rent
|
|
|
|
|
The condition in the company is to bind them from xaml but when I am trying to bind the ViewModel from xaml, I am getting value only if I bind one but when I am trying to bind the second one and try to raise NotifyPropertyChanged event then both the values are not coming inth xaml.
Then I tried add static resource of ViewModel in xaml, the ViewModel has the same namespace and assembly as xaml view file, its not letting me add the static resource of the ViewModel.
Without ViewModel is not being accessed as StaticResource I am not able to reference the ViewModels Properties in the View, its giving me compile error saying that ViewModel is not available in the namespace. I tried it many ways not not helping me out, I wrote the code as above question.
Anyhelp please.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
Without you showing your ViewModel and View here, it's impossible to say why it doesn't work, however I would take a guess that you aren't actually referencing the namespace in your XAML. Typically, I would expect to see something like this in your Window/UserControl header:
xmlns:vm="clr-namespace:MyApplication.ViewModelNamespace" Then you would reference this as such
<Window.DataContext><vm:NameOfViewModel></Window.Resources>
This space for rent
|
|
|
|
|
Hi,
My here is my View
<Window x:Class="TestWPFApplication.MainWindow"<br />
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:testns="clr-namespace:TestWPFApplication"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
>
<Window.Resources><br />
<testns:TestViewModel x:Key="_testViewModel" Name="Abdul">
</testns:TestViewModel>
</Window.Resources>
<Grid>
<Label Content="Enter Name" HorizontalAlignment="Left" Margin="58,62,0,0" VerticalAlignment="Top" Name="lblName"/>
<TextBox Text="{Binding Name, Mode=OneWay}" HorizontalAlignment="Left" Height="22" Margin="160,64,0,0" TextWrapping="Wrap"
Name="txtName" VerticalAlignment="Top" Width="120"/>
<pre>
<Button Content="Save" HorizontalAlignment="Left" Margin="277,142,0,0" VerticalAlignment="Top" Width="74" Name="btnSaveSpecific"
Command="{Binding Path=TestSpecificCommand}"
CommandParameter="{Binding ElementName=txtName, Path=Text}" />
<Label Content="Enter Id" HorizontalAlignment="Left" Margin="58,17,0,0" VerticalAlignment="Top" Name="lblId"/>
<TextBox Text="{Binding Name, Mode=OneWay}" HorizontalAlignment="Left" Height="22" Margin="160,18,0,0" TextWrapping="Wrap"
Name="txtId" VerticalAlignment="Top" Width="120"/>
<Button Content="Save 2" HorizontalAlignment="Left" Margin="412,142,0,0" VerticalAlignment="Top" Width="74" Name="btnSaveSpecific2"
Command="{Binding Path=ClickCommand}"
/>
<DataGrid HorizontalAlignment="Left" Margin="103,188,0,0" VerticalAlignment="Top" Height="98" Width="313"
IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=PersonNames, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name of Employee" Binding="{Binding Path=Name}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
And ViewModel
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace TestWPFApplication
{
public class TestViewModel : INotifyPropertyChanged
{
TestData _testData;
public TestSpecificCommand TestSpecificCommand { get; set; }
public TestViewModel()
{
_testData = new TestData();
_personNames = new ObservableCollection<TestData>();
this.TestSpecificCommand = new TestSpecificCommand(this);
}
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value; NotifyPropertyChanged("Name");
}
}
ObservableCollection<TestData> _personNames;
public ObservableCollection<TestData> PersonNames
{
get { return _personNames; }
set
{
_personNames = value; NotifyPropertyChanged("PersonNames");
}
}
ObservableCollection<Tuple<int, TestData>> _dictionaryNames;
ObservableCollection<Tuple<int, TestData>> DictionaryNames
{
get { return _dictionaryNames; }
set
{
_dictionaryNames = value; NotifyPropertyChanged("DictionaryNames");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{<br />
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
private ICommand _clickCommand;
public ICommand ClickCommand
{
get
{
return _clickCommand ?? (_clickCommand = new TestSpecificCommand(() => AddAnItem(), _canExecute));
}
}
public bool _canExecute { get; set; }
private int _id;
public int Id
{
get { return _id; }
set
{
_id = value; NotifyPropertyChanged("Id");
}
}
public void AddAnItem()
{<br />
_personNames.Add(new TestData(Id, _name));
}
public void AddAnItem(string _name)
{
int t;
if ((_personNames == null) || (_personNames.Count() <= 0))
t = 1;
else
t = (_personNames.Count <= 0) ? 1 : _personNames.Max(x => x.Id) + 1;
_personNames.Add(new TestData(t, _name));
}
}
}
When I am referencing it, it is saying that the TestViewModel is not present in the clr-namespace:TestWPFApplication namespace and saying the UI above that Invalid Markup, can you please let me know buddy what's wrong in it?
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
TestViewModel is not a resource, yet you are attempting to use it as though it is. Change your Window.Resources section to a Window.DataContext that looks like this
<Window.DataContext>
<testns:TestViewModel />
</Window.DataContext>
This space for rent
|
|
|
|
|
Hi,
I changed it as below, but still it is giving me the same error, any help please, still it is saying me that and in the UI it is saying invalid xaml.
Error 1 The name "TestViewModel" does not exist in the namespace "clr-namespace:TestWPFApplication.ViewModel". \\VBOXSVR\VMShared\Test Projects\TestWPFApplication\TestWPFApplication\View\MainWindow.xaml 9 9 TestWPFApplication
My code is below, any help please?
<Window x:Class="TestWPFApplication.MainWindow"<br />
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:testns="clr-namespace:TestWPFApplication.ViewModel"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" >
<Window.DataContext>
<testns:TestViewModel Name="Abdul">
</testns:TestViewModel>
</Window.DataContext>
<Grid>
<Label Content="Enter Name" HorizontalAlignment="Left" Margin="58,62,0,0" VerticalAlignment="Top" Name="lblName"/>
<TextBox Text="{Binding Name, Mode=OneWay}" HorizontalAlignment="Left" Height="22" Margin="160,64,0,0" TextWrapping="Wrap"
Name="txtName" VerticalAlignment="Top" Width="120"/>
<pre>
<Button Content="Save" HorizontalAlignment="Left" Margin="277,142,0,0" VerticalAlignment="Top" Width="74" Name="btnSaveSpecific"
Command="{Binding Path=TestSpecificCommand}"
CommandParameter="{Binding ElementName=txtName, Path=Text}" />
<DataGrid HorizontalAlignment="Left" Margin="103,188,0,0" VerticalAlignment="Top" Height="98" Width="313"
IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=PersonNames, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name of Employee" Binding="{Binding Path=Name}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
Now I changed a little bit the ViewModel I put in a folder called ViewModel and Model in Model folder and changed their namespaces and changed the reference of clr-namespace as well but still the error persists.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
modified 16-May-16 20:53pm.
|
|
|
|
|
And that would be correct. Look at the code you posted. The ViewModel isn't in that namespace, it's in TestWPFApplication. The error was very explicit about what the problem was.
This space for rent
|
|
|
|
|
Thanks for all your help but I found the Problem that I was struggling with, it was because of the network drive I loading the Application path from. When we are using loading the static resources data context the UI can take them only if they from trusted source, my application was on network drive.
Thanks for all your support Pete and Rick, without you guys I couldn't have got this far. About my wrong code here I was changing the code all the time, may be pasted it when it was wrong. Thank you very much for that.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
Glad you got there in the end.
This space for rent
|
|
|
|
|