1

I am trying to set Property IsWriteAllowed of SIDJavnaUstanovaViewModel to TextBox. I cannot do that, and I tried Relative Source, ElementName but it just does not works. I am trying to set IsWriteAllowed property to TextBox IsEnabled Property. In XAML-s where I have StaticResource, this is working fine, but here, when I set DataContext programmaticaly, I can't bind it. I have set DataContext programmatically:

SIDJavnaUstanovaViewModel definitionvm = new SIDJavnaUstanovaViewModel(); definitionvm.FillElements(null); Definition definition = new Definition(); // Create new XAML definition.DataContext = definitionvm; // Set its DataContext definition.Show(); 

And XAML looks like this:

<src:BaseWindow x:Class="StoreIDCard.Definition" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" DataContext="{Binding RelativeSource={RelativeSource Self}}" // i tried without that, but nothing happens xmlns:src="clr-namespace:StoreIDCard.Base" xmlns:ComponentModel="clr-namespace:System.ComponentModel;assembly=PresentationFramework" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit" xmlns:viewmod="clr-namespace:StoreIDCard.ViewModel" x:Name="DefinitionWin" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" ShowInTaskbar="True" xmlns:my="clr-namespace:StoreIDCard.View" Width="521" Height="494" Icon="/StoreIDCard;component/Images/Delhaize.png" IsEnabled="{Binding}"> <Grid Height="465" Width="503" > <Grid.ColumnDefinitions> <ColumnDefinition Width="476*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Button Content="{StaticResource save}" Command="{Binding SaveAndUpdate}" Height="34" HorizontalAlignment="Left" Margin="188,411,0,0" VerticalAlignment="Top" Width="133" /> <DataGrid AutoGenerateColumns="False" Block.TextAlignment="Left" Height="331" Visibility="{Binding Path=IsVisible}" ItemsSource="{Binding Path=Elements}" Name="dataGrid2" SelectedItem="{Binding SelectedElement}" VerticalAlignment="Top" HorizontalAlignment="Left" Width="371" UseLayoutRounding="True" VerticalScrollBarVisibility="Auto" Margin="73,53,0,0"> <DataGrid.Columns> <DataGridTemplateColumn Header="{StaticResource name}" Width="320"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBox Text="{Binding Naziv, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Background="{Binding BackgroundColor}" IsReadOnly="{Binding Path=IsReadonly}" ////CANNOT SET ISWRITEALLOWED HERE, TRIED EVERYTHING IsEnabled="{Binding Path=IsWriteAllowed, RelativeSource={RelativeSource Self}}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> <DataGrid.ContextMenu> <ContextMenu> // ISWRITEALLOWED WORKING PERFECTLY Here <MenuItem Command="{Binding Delete}" IsEnabled="{Binding Path=IsWriteAllowed}" Header="{StaticResource delete}"/> <MenuItem ClipToBounds="True" Command="{Binding Add}" Header="{StaticResource add}"/> </ContextMenu> 
0

2 Answers 2

1

The answer you have provided is correct, but I thought I would provide another alternative. If you are using .NET 4 or later, you may use the x:Reference markup.

I can see your root window is named DefinitionWin, so you may use x:Reference like so:

IsEnabled="{Binding DataContext.IsWriteAllowed, Source={x:Reference DefinitionWin}}" 

This alleviates WPF from having to travel the visual tree.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I tried and it is working too, and seems like better solution. Thanks.
0

Well I seemed to find the solution... I have searched, and searched again...

IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.IsWriteAllowed}" 

https://stackoverflow.com/questions/1127933/wpf-databinding...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.