我有一个数据网格,我根据从另一列中选择的内容更改单元格。
让我们说,我的第一列显示工作日(枚举)。如果用户选择“星期一”,则第二列中的单元格将是TextBox。如果用户选择不同的一天(例如星期五),它将成为Text = Text =“Hooray!”,否则Textblock Text =“”。
Textbox和Textblock都绑定在同一属性中。但是这种绑定不起作用。请帮忙......
XAML
<DataGrid Margin="0,10,0,0"
x:Name="dataGrid"
AutoGenerateColumns="False"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding TheCollection}"
SelectedItem="{Binding TheSelectedItemFromTheCollection}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Days"
CanUserReorder="False"
CanUserResize="False"
CellTemplate="{StaticResource local.DataGridDays}" />
<DataGridTemplateColumn Header="Value"
CanUserReorder="False"
CanUserResize="False"
CellTemplate="{StaticResource local.DataGridValue}" />
</DataGrid.Columns>
</DataGrid>
<DataTemplate x:Key="local.DataGridValuesEditable">
<StackPanel>
<Border Background="White"
BorderThickness="1"
Padding="4">
<TextBox Text="{Binding InitialValue, RelativeSource={RelativeSource Self}, UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Bottom"
Margin="0" />
</Border>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="local.DataGridValuesDefault">
<StackPanel>
<Border Background="White"
BorderThickness="1"
Height="30"
Padding="4">
<TextBlock Text="{Binding InitialValue, Mode=TwoWay, RelativeSource={RelativeSource Self}, UpdateSourceTrigger=PropertyChanged}"
VerticalAlignment="Bottom"
Margin="0"
Width="55" />
</Border>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="local.DataGridValue">
<ContentControl>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate"
Value="{DynamicResource local.DataGridValuesDefault}" />
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedDay, UpdateSourceTrigger=PropertyChanged}"
Value="Monday">
<Setter Property="ContentTemplate"
Value="{DynamicResource local.DataGridValuesEditable}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
视图模型
private ObservableCollection<TheModel> _theCollection;
public ObservableCollection<TheModel> TheCollection
{
get
{
if (_theCollection == null)
_theCollection = new ObservableCollection<TheModel>();
return _theCollection;
}
set
{
_theCollection = value;
}
}
private TheModel _theSelectedItemFromTheCollection;
public TheModel TheSelectedItemFromTheCollection
{
get
{
if (_theSelectedItemFromTheCollection == null)
_theSelectedItemFromTheCollection = new TheModel();
return _theSelectedItemFromTheCollection;
}
set
{
_theSelectedItemFromTheCollection = value;
NotifyPropertyChanged("TheSelectedItemFromTheCollection");
}
}
模型
public string SelectedDay
{
get { return day; }
set
{
day = value;
if (day == TheWeekDays.Friday.ToString())
{
InitialValue = "Hooray!!";
}
else
InitialValue = string.Empty;
NotifyPropertyChanged("SelectedDay");
}
}
private string _initialValue;
public string InitialValue
{
get { return _initialValue; }
set
{
_initialValue = value;
NotifyPropertyChanged("InitialValue");
}
}
public IEnumerable<WeekDays> TheWeekDays
{
get
{
return Enum.GetValues(typeof(WeekDays))
.Cast<WeekDays>();
}
}
答案 0 :(得分:0)
以下是所需的情况:
DataContext
和local.DataGridValuesDefault
的{{1}}应该是行项local.DataGridValuesEditable
。
使用此TheModel
,绑定可以写成如下:
DataContext
和
<TextBox Text="{Binding InitialValue,UpdateSourceTrigger=PropertyChanged}"
<TextBlock Text="{Binding InitialValue}"
的{{1}}包含所需的项目。 DataContext
会将其local.DataGridValue
转换为其ContentControl
的{{1}},因此Content
需要与外DataContext
相关联。此外,触发器绑定可以简化:
ContentTemplate