UserControl 内容仅在第一次显示时更新
UserControl Contents only updates the first time it is shown
我在自定义 UserControl 中遇到数据绑定问题。
如果在用户输入中发现错误,则它几乎是一个错误对话框覆盖层,它会在按下按钮时被调用。
在控件中我有 2 个对象,一个退出按钮和一个包含错误的字符串列表。
我的问题是用户控件上的列表是在对话框可见性第一次设置为 true 时设置的,之后就不会刷新。
我在更改列表时调用了 OnNotifyPropertyChange,但它似乎仍然没有任何区别。
我的 MainContent XAML UserContent 部分。
<Grid x:Name="Overlay" Panel.ZIndex="1000" Visibility="{Binding Path=ShowOverlay, Converter={StaticResource booltoVis},FallbackValue=Hidden }" Grid.Row="2" Grid.RowSpan="2">
<Grid.Background>
<SolidColorBrush Color="Black" Opacity=".5"/>
</Grid.Background>
<View:UserControl1>
<View:UserControl1.MainContent>
<ListBox ItemsSource="{Binding Path=Error_Message_List, Mode=TwoWay}" />
</View:UserControl1.MainContent>
<View:UserControl1.DialogExitButton>
<Button Command="{Binding Path=CloseModalDialogClickCommand}" Content="OK">
</Button>
</View:UserControl1.DialogExitButton>
</View:UserControl1>
</Grid>
我的用户控件绑定 XAML
<Grid Margin="5" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="0.1*"/>
<RowDefinition Height="0.6*"/>
<RowDefinition Height="0.2*"/>
</Grid.RowDefinitions>
<TextBlock Text="Error in script generation!" FontWeight="Bold"/>
<ContentControl Content="{Binding MainContent, ElementName=XAMLErrorPopupControl}" Grid.Row="1"/>
<ContentControl Content="{Binding DialogExitButton, ElementName=XAMLErrorPopupControl}" Grid.Row="2" Margin="224,0,0,0"/>
</Grid>
我的用户控件代码隐藏
public static readonly DependencyProperty MainContentProperty =
DependencyProperty.Register(
"MainContent",
typeof(object),
typeof(UserControl1),
new FrameworkPropertyMetadata(default(object),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public object MainContent
{
get { return (object)GetValue(MainContentProperty); }
set { SetValue(MainContentProperty, value); OnPropertyChanged("MainContent"); }
}
public static readonly DependencyProperty DialogExitButtonProperty =
DependencyProperty.Register(
"DialogExitButton",
typeof(object),
typeof(UserControl1),
new UIPropertyMetadata(null));
public object DialogExitButton
{
get { return (object)GetValue(DialogExitButtonProperty); }
set { SetValue(DialogExitButtonProperty, value); OnPropertyChanged("DialogExitButton"); }
}
如有任何帮助,我们将不胜感激!干杯!
首先: Error_Message_List
必须是 ObservableCollection<YourItemType>
才能在项目 [ 时通知 UI =26=] 来自列表。
其次: YourItemType
必须实现 INotifyPropertyChanged
以在列表中的项目被 修改时通知 UI.
我在自定义 UserControl 中遇到数据绑定问题。
如果在用户输入中发现错误,则它几乎是一个错误对话框覆盖层,它会在按下按钮时被调用。
在控件中我有 2 个对象,一个退出按钮和一个包含错误的字符串列表。
我的问题是用户控件上的列表是在对话框可见性第一次设置为 true 时设置的,之后就不会刷新。 我在更改列表时调用了 OnNotifyPropertyChange,但它似乎仍然没有任何区别。
我的 MainContent XAML UserContent 部分。
<Grid x:Name="Overlay" Panel.ZIndex="1000" Visibility="{Binding Path=ShowOverlay, Converter={StaticResource booltoVis},FallbackValue=Hidden }" Grid.Row="2" Grid.RowSpan="2">
<Grid.Background>
<SolidColorBrush Color="Black" Opacity=".5"/>
</Grid.Background>
<View:UserControl1>
<View:UserControl1.MainContent>
<ListBox ItemsSource="{Binding Path=Error_Message_List, Mode=TwoWay}" />
</View:UserControl1.MainContent>
<View:UserControl1.DialogExitButton>
<Button Command="{Binding Path=CloseModalDialogClickCommand}" Content="OK">
</Button>
</View:UserControl1.DialogExitButton>
</View:UserControl1>
</Grid>
我的用户控件绑定 XAML
<Grid Margin="5" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="0.1*"/>
<RowDefinition Height="0.6*"/>
<RowDefinition Height="0.2*"/>
</Grid.RowDefinitions>
<TextBlock Text="Error in script generation!" FontWeight="Bold"/>
<ContentControl Content="{Binding MainContent, ElementName=XAMLErrorPopupControl}" Grid.Row="1"/>
<ContentControl Content="{Binding DialogExitButton, ElementName=XAMLErrorPopupControl}" Grid.Row="2" Margin="224,0,0,0"/>
</Grid>
我的用户控件代码隐藏
public static readonly DependencyProperty MainContentProperty =
DependencyProperty.Register(
"MainContent",
typeof(object),
typeof(UserControl1),
new FrameworkPropertyMetadata(default(object),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public object MainContent
{
get { return (object)GetValue(MainContentProperty); }
set { SetValue(MainContentProperty, value); OnPropertyChanged("MainContent"); }
}
public static readonly DependencyProperty DialogExitButtonProperty =
DependencyProperty.Register(
"DialogExitButton",
typeof(object),
typeof(UserControl1),
new UIPropertyMetadata(null));
public object DialogExitButton
{
get { return (object)GetValue(DialogExitButtonProperty); }
set { SetValue(DialogExitButtonProperty, value); OnPropertyChanged("DialogExitButton"); }
}
如有任何帮助,我们将不胜感激!干杯!
首先: Error_Message_List
必须是 ObservableCollection<YourItemType>
才能在项目 [ 时通知 UI =26=] 来自列表。
其次: YourItemType
必须实现 INotifyPropertyChanged
以在列表中的项目被 修改时通知 UI.