我正在尝试将 SIDJavnaUstanovaViewModel 的属性 IsWriteAllowed 设置为 TextBox。我不能这样做,我尝试了相对源元素名称,但它不起作用。我正在尝试将 IsWriteAllowed 属性设置为 TextBox IsEnabled 属性。在我有 StaticResource 的 XAML-s 中,这工作正常,但是在这里,当我以编程方式设置 DataContext 时,我无法绑定它。我以编程方式设置了 DataContext:
SIDJavnaUstanovaViewModel definitionvm = new SIDJavnaUstanovaViewModel();
definitionvm.FillElements(null);
Definition definition = new Definition(); // Create new XAML
definition.DataContext = definitionvm; // Set its DataContext
definition.Show();
XAML 看起来像这样:
<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>