C# 绑定未在datagrid内更新
我有一个数据网格,根据从另一列中选择的内容更改单元格 比如说,我的第一列显示了工作日(枚举)。如果用户选择“星期一”,则第二列中的单元格将是一个文本框。如果用户选择不同的日期(如星期五),它将变成一个文本块,并带有Text=“Hooray!”,else Textblock Text=“” Textbox和Textblock都绑定在同一个属性中。但这种绑定不起作用。请帮忙 XAMLC# 绑定未在datagrid内更新,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我有一个数据网格,根据从另一列中选择的内容更改单元格 比如说,我的第一列显示了工作日(枚举)。如果用户选择“星期一”,则第二列中的单元格将是一个文本框。如果用户选择不同的日期(如星期五),它将变成一个文本块,并带有Text=“Hooray!”,else Textblock Text=“” Textbox和Textblock都绑定在同一个属性中。但这种绑定不起作用。请帮忙 XAML 视图模型 private ObservableCollection<TheModel> _theCo
视图模型
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");
}
}
private observedcollection\u集合;
公众可观察的集合
{
得到
{
if(_theCollection==null)
_集合=新的可观察集合();
返回集合;
}
设置
{
_集合=值;
}
}
private TheModel(从集合中选择的编辑);
将从集合中选择的编辑项的模型公开
{
得到
{
如果(_theSelectedItemFromTheCollection==null)
_从集合中选择的编辑项=新建模型();
返回_从集合中选择的编辑项;
}
设置
{
_从集合中选择的编辑项=值;
NotifyPropertyChanged(“从集合中选择的编辑”);
}
}
模型
public string SelectedDay
{
获取{返回日;}
设置
{
天=价值;
如果(day==工作日.Friday.ToString())
{
InitialValue=“万岁!!”;
}
其他的
InitialValue=string.Empty;
NotifyPropertyChanged(“SelectedDay”);
}
}
私有字符串_initialValue;
公共字符串初始值
{
获取{return\u initialValue;}
设置
{
_初始值=值;
NotifyPropertyChanged(“初始值”);
}
}
公平交易日
{
得到
{
返回Enum.GetValues(typeof(工作日))
.Cast();
}
}
以下是理想情况:
local.DataGridValuesDefault
和local.datagridvaluesditable
的DataContext
应该是model的行项目
使用此DataContext
,可以按如下方式编写绑定:
<TextBox Text="{Binding InitialValue,UpdateSourceTrigger=PropertyChanged}"
{Binding InitialValue,RelativeSource={RelativeSource Self}}
你能解释一下为什么你希望这能起作用吗?首先,没有RelativeSource={RelativeSource Self}。但后来我试着把它放进去,但它仍然不起作用。我还需要更改什么?查看visual studio输出窗口,它将显示一些失败绑定的错误文本。我的猜测是:您需要将ContentControl.Content
属性设置为Content=“{Binding}”
,但我不是100%确定。而且不要忘记删除RelativeSource
。我做了以下操作:删除了RelativeSource,更改了
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>();
}
}
<TextBox Text="{Binding InitialValue,UpdateSourceTrigger=PropertyChanged}"
<TextBlock Text="{Binding InitialValue}"
<DataTemplate x:Key="local.DataGridValue">
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate"
Value="{DynamicResource local.DataGridValuesDefault}" />
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedDay}"
Value="Monday">
<Setter Property="ContentTemplate"
Value="{DynamicResource local.DataGridValuesEditable}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>