我想将FirstDate和SecondDate Visibility的StackPanel属性绑定到父{m} DataContext,而是绑定到当前元素的DataContext。如何忽略此DataContext并设置与父DataContext的约束?
此外,MonthList必须忽略父DataContext,但它不会这样做。
<StackPanel
DataContext="{Binding dateOfProduction}"
Orientation="Vertical"
Grid.Row="10"
Grid.Column="1">
<StackPanel Orientation="Horizontal">
<RadioButton Content="One date" Margin="5,0" IsChecked="{Binding Path=DateType, Converter={StaticResource enumBooleanConverter}, ConverterParameter=OneDate}"/>
<RadioButton Content="Two date" Margin="5,0" IsChecked="{Binding Path=DateType, Converter={StaticResource enumBooleanConverter}, ConverterParameter=TwoDate}"/>
<RadioButton Content="Not exists" Margin="5,0" IsChecked="{Binding Path=DateType, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Empty}"/>
</StackPanel>
<StackPanel DataContext="{Binding Path=FirstDate}" Visibility="{Binding FirstDateExist, Converter={StaticResource booleanToVisibilityConverter}}" Orientation="Horizontal">
<Label Width="37" Content="Day" />
<TextBox VerticalContentAlignment="Center" Width="19" Text="{Binding Path=Day}"/>
<Label Width="46" Content="Month" />
<ComboBox Width="60" ItemsSource="{Binding MonthList}" SelectedIndex="{Binding Path=Month}"/>
<Label Width="29" Content="Year" />
<TextBox VerticalContentAlignment="Center" Width="32" Text="{Binding Path=Year}"/>
<Label Width="30" Content="Hour" />
<TextBox VerticalContentAlignment="Center" Width="19" Text="{Binding Path=Hour}"/>
<Label Width="51" Content="Minute" />
<TextBox VerticalContentAlignment="Center" Width="19" Text="{Binding Path=Minute}"/>
</StackPanel>
<StackPanel DataContext="{Binding Path=SecondDate}" Visibility="{Binding SecondDateExist, Converter={StaticResource booleanToVisibilityConverter}}" Orientation="Horizontal">
<Label Width="37" Content="Day" />
<TextBox VerticalContentAlignment="Center" Width="19" Text="{Binding Path=Day}"/>
<Label Width="46" Content="Month" />
<ComboBox Width="60" ItemsSource="{Binding MonthList}" SelectedIndex="{Binding Path=Month}"/>
<Label Width="29" Content="Year" />
<TextBox VerticalContentAlignment="Center" Width="32" Text="{Binding Path=Year}"/>
<Label Width="30" Content="Hour" />
<TextBox VerticalContentAlignment="Center" Width="19" Text="{Binding Path=Hour}"/>
<Label Width="51" Content="Minute" />
<TextBox VerticalContentAlignment="Center" Width="19" Text="{Binding Path=Minute}"/>
</StackPanel>
</StackPanel>
您可以在下面看到使用的课程
public class GoodsDate : PropertyChangedModels
{
private ComplexDate _FirstDate;
public ComplexDate FirstDate
{
get { return _FirstDate; }
set { _FirstDate = value; OnPropertyChanged("FirstDate"); }
}
private ComplexDate _SecondDate;
public ComplexDate SecondDate
{
get { return _SecondDate; }
set { _SecondDate = value; OnPropertyChanged("SecondDate"); }
}
public bool FirstDateExist
{
get => (FirstDate != null);
}
public bool SecondDateExist
{
get => (SecondDate != null);
}
}
public class ComplexDate : PropertyChangedModels
{
public ComplexDate(int? year = null, int? month = null, int? day = null, int? hour = null, int? minute = null)
{
Year = year;
Month = month;
Day = day;
Hour = hour;
Minute = minute;
}
private int? _Year;
public int? Year
{
get { return _Year; }
set { _Year = value?.LimitToRange(1000, 2999); OnPropertyChanged("Year"); }
}
private int? _Month;
public int? Month
{
get { return _Month; }
set { _Month = value?.LimitToRange(1, 12); OnPropertyChanged("Month"); }
}
private int? _Day;
public int? Day
{
get { return _Day; }
set { _Day = value?.LimitToRange(1, 31); OnPropertyChanged("Day"); }
}
private int? _Hour;
public int? Hour
{
get { return _Hour; }
set { _Hour = value?.LimitToRange(0, 23); OnPropertyChanged("Hour"); }
}
private int? _Minute;
public int? Minute
{
get { return _Minute; }
set { _Minute = value?.LimitToRange(0, 59); OnPropertyChanged("Minute"); }
}
}
答案 0 :(得分:2)
因为,您已在First / SecondDate上将DataContext定义为DataContext="{Binding Path=FirstDate}"
,因此Visibility
属性正在尝试从set DataContext中解析它。
您的问题有各种解决方案。您可以实现上述
之一<强> 1 强> 使用Visiblity属性上的相对源将绑定设置为Parent。
Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=FirstDateExist, Converter={StaticResource booleanToVisibilityConverter}}"
2。
将StackPanel包装到网格并在其上设置Visiblity。在这种情况下,Visiblity将绑定到Top stackpanel的DataContext。
<Grid Visibility="{Binding FirstDateExist, Converter={StaticResource booleanToVisibilityConverter}}">
<StackPanel DataContext="{Binding Path=FirstDate}" Orientation="Horizontal">
<Label Width="37" Content="Day" />
<TextBox VerticalContentAlignment="Center" Width="19" Text="{Binding Path=Day}"/>
<Label Width="46" Content="Month" />
<ComboBox Width="60" ItemsSource="{Binding MonthList}" SelectedIndex="{Binding Path=Month}"/>
<Label Width="29" Content="Year" />
<TextBox VerticalContentAlignment="Center" Width="32" Text="{Binding Path=Year}"/>
<Label Width="30" Content="Hour" />
<TextBox VerticalContentAlignment="Center" Width="19" Text="{Binding Path=Hour}"/>
<Label Width="51" Content="Minute" />
<TextBox VerticalContentAlignment="Center" Width="19" Text="{Binding Path=Minute}"/>
</StackPanel>
</Grid>
第3 强>
删除DataContext="{Binding Path=FirstDate}"
并将级联控件绑定更改为Text="{Binding FirstDate.Day}"