Click here to Skip to main content
15,913,100 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
I use this Service:
C#
public Service()
{
        BList = new ObservableCollection<object>()
            {new CommonData() { MenuName="name1",Id="s01"}
            ,new BOption() {MenuName="nam2"}
            ,new BOption(){MenuName="nam3"}};
}
        public ObservableCollection<object> BList { get; private set; }

Then this ViewModel:
C#
public class CommonScreenViewModel
    {
        public ViewModel(IService Service)
        {
            this.Service=Service;
            this.ImportCommand = new DelegateCommand<object>(this.OnImport, this.CanImport);
        }

        public IService Service;
        public ICommand ImportCommand { get; private set; }
        private void OnImport(object arg) {
            MessageBox.Show("Importing...");
        }
        private bool CanImport(object arg) { return true; }
    }


I set the dataContext in the View.xaml.cs like this:
C#
public View(ViewModel viewModel)
        {
            InitializeComponent();
            this.Loaded += (s, e) =>
            { this.DataContext = viewModel; };
        }


In the XAML file i want to do some binding like this:
XML
<Button Content="Command" IsEnabled="{Binding CanImport}" >
        <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                <i:InvokeCommandAction Command="{Binding ImportCommand}"/>
                </i:EventTrigger>
        </i:Interaction.Triggers>
</Button>
<TextBox Text="{Binding Path=Service.BList[0].Id, Mode=TwoWay}"/>


My problem is that the Button Binding works fine but the textbox does not.

If I change the view.xaml.cs to
C#
public View(ViewModel viewModel)
        {
            InitializeComponent();
            this.Loaded += (s, e) =>
            { this.DataContext = viewModel.Service.BList[0]; };
        }

and bind my textbox with just
XML
<TextBox Text="{Binding Path=Id, Mode=TwoWay}"/>


then the TextBox binds fine but the button doen't work

Any suggestions? (thnx in advance)
Posted

I have not tried using arrays in bindings. What may work is just having a property that accesses the first element. If that works, can go from there.
 
Share this answer
 
I solved it (inspired from Clifford Nelson's suggetion) this way:

the ViewModel:
C#
public class ViewModel
    {
        public ViewModel(IService Service)
        {
            this.Service=Service;
            this.MyData=Service.BList[0] as CommonData;
            this.ImportCommand = new DelegateCommand<object>(this.OnImport, this.CanImport);
        }
 
        public IService Service;
        public CommonData MyData {get;set;}
        public ICommand ImportCommand { get; private set; }
        private void OnImport(object arg) {
            MessageBox.Show("Importing...");
        }
        private bool CanImport(object arg) { return true; }
    }

and then Bind in the XAML like this:
XML
<textbox text="{Binding Path=MyData.Id, Mode=TwoWay}" />
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900