UWP x:Bind twoway需要转换器吗?

时间:2015-11-30 16:39:57

标签: uwp windows-10-universal xbind

我正在使用绑定到我的ViewModel.SeletedLogItem属性的Content控件,该属性的类型为LogItemDTO,它在DataTemplate的DataType中指定。当我将绑定设置为Mode=TwoWay时,我收到编译错误,说我需要转换器。

  

无效的绑定路径' ViewModel.SelectedLogItem' :无法绑定类型' LifeLog.Data.DomainEntities.LogItemDTO'到' System.Object'没有转换器

以下是我的viewmodel的一部分和内容控件的xaml

public class LogItemEditPageViewModel : LifeLog.App.Mvvm.ViewModelBase
{
    #region Properties

    private LogItemInfo OriginalData { get; set;}

    private LogItemDTO selectedLogItem;
    public LogItemDTO SelectedLogItem { get { return selectedLogItem; } set { Set(ref selectedLogItem, value); } }
<ContentControl Margin="20,0,0,0" Grid.Row="2" Grid.Column="0" Content="{x:Bind ViewModel.SelectedLogItem, Mode=TwoWay}">
<ContentControl.ContentTemplate>
    <DataTemplate  x:DataType="dat:LogItemDTO">
        <StackPanel>
            <TextBox Text="{x:Bind Log, Mode=TwoWay}" FontSize="15" Height="450" Width="728"
                        TextWrapping="Wrap"
                        AcceptsReturn="True"
                        Foreground="{StaticResource Yell}" Background="{StaticResource DGreen}"
                        />
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                <StackPanel Orientation="Horizontal" Margin="0,10, 10,10" >
                    <TextBlock Text="LastUpdated:" Margin="0,0,5,0" />
                    <TextBlock Text="{x:Bind LastUpdated}" Foreground="{StaticResource DGreen}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" Margin="0,10" >
                    <TextBlock Text="Date Added:" Margin="0,0,5,0" />
                    <TextBlock Text="{x:Bind DateAdded}" Foreground="{StaticResource DGreen}"/>
                </StackPanel>
            </StackPanel>
        </StackPanel>
    </DataTemplate>
</ContentControl.ContentTemplate>

1 个答案:

答案 0 :(得分:0)

您可以使用转换器进行绑定,但是您需要编写转换器

类似的东西:

public class XConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter)
            {//return converted value
                return (
             //do something here                
               }
        }

然后绑定

<TextBox  Converter={StaticResource XConverter}></TextBox>