그리드 내부에 usercontrol을 사용하고 있습니다. 이 사용자 정의 컨트롤은 viewmodel에서 제공하는 객체의 속성에 바인딩 된 텍스트 상자를 포함합니다. 내 문제는 속성이 오히려 문자열 또는 int 수 있습니다.usercontrol의 MVVM 텍스트 상자가 다른 유형으로 바인딩
종속성 속성을 문자열로 설정하여 표시하기에 적합하지만 사용자가 int 필드에 문자열을 입력하면보기 모델에 의해 유효성 검사 오류가 발생하지 않지만 해당 필드에 저장할 수 없습니다. 데이터 베이스.
에서 UserControl XAML
<UserControl x:Class="GenericFileTransferClient.Views.UserControlTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid Name="GridLayout">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="7*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Path=LabelText}" VerticalAlignment="Center" Style="{StaticResource ResourceKey=TextBlockWrapping}" />
<DockPanel Grid.Column="1">
<Border DockPanel.Dock="Right"
ToolTip="" Style="{StaticResource BorderReportDetail}">
<TextBlock Text="?" Style="{StaticResource TextBoxReportDetail}"></TextBlock>
</Border>
<TextBox Text="{Binding Path=TextBoxText}"/>
</DockPanel>
</Grid>
</UserControl>
UserControl을 코드의 주요 그리드
<views:UserControlTextBox LabelText="Report Name:" TextBoxText="{Binding Path=CurrentReport.ReportName}" Grid.Row="1" Grid.ColumnSpan="2"/>
<TextBlock Text="Header:" Grid.Row="2"/>
<CheckBox Grid.Column="1" Grid.Row="2" IsChecked="{Binding Path=CurrentReport.Header}"
VerticalContentAlignment="Stretch" VerticalAlignment="Center" Name="HeaderCheckBox"/>
<Grid Grid.Row="3" Grid.ColumnSpan="2" Visibility="{Binding IsChecked, ElementName=HeaderCheckBox, Converter={StaticResource BoolToVisConverter}}">
<views:UserControlTextBox LabelText="# Row Header:" TextBoxText="{Binding Path=CurrentReport.HeaderRow}"/>
</Grid>
당신이 볼 수 있듯이에서 UserControl을의 호출의
public partial class UserControlTextBox : UserControl
{
public static DependencyProperty LabelTextProperty = DependencyProperty.Register("LabelText", typeof(String), typeof(UserControl));
public static DependencyProperty TextBoxTextProperty = DependencyProperty.Register("TextBoxText", typeof(String), typeof(UserControl));
public String LabelText
{
get { return (String)GetValue(LabelTextProperty); }
set { SetValue(LabelTextProperty, value); }
}
public String TextBoxText
{
get { return (String)GetValue(TextBoxTextProperty); }
set { SetValue(TextBoxTextProperty, value); }
}
public UserControlTextBox()
{
InitializeComponent();
GridLayout.DataContext = this;
}
}
예 뒤에, CurrentReport.ReportName는이다 문자열이지만 CurrentReport.HeaderRow는 int입니다.
종속성 속성을 일반화하거나 usercontrol에 전달 된 매개 변수를 기반으로하는 방법이 있습니까? 또는 사용자가 저장 버튼을 클릭하기 전에 유효성 검사를 수행 할 수있는 방법이 있습니까?
는
답장을 보내 주셔서 감사합니다. 이전에 생각해 보았던 것이 쉬운 생각입니다. 난 그냥 IntToStringConverter 구현하지만 내 convertback 해고되지 않습니다 몇 가지 이유로. 나는 이유를 조사하고 나중에 내 대답을 편집 할 것입니다. – Regis