即使使用单向模式设置也只有财产
视图模型
namespace My.ViewModels
{
    public class ItemViewModel : ObservableObject
    {
        private ItemModel _model;
        public ItemViewModel(ItemModel model)
        {
            _model = model;
        }
        public string Name { get { return _model.Name; } }
    }
}
XAML
<UserControl x:Class="My.Controls.ItemControl"
             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"
             xmlns:viewModels="clr-namespace:My.ViewModels"
             mc:Ignorable="d"
             d:DesignHeight="421" d:DesignWidth="786"
             d:DataContext="{d:DesignInstance viewModels:ItemViewModel}">
    <Grid Background="White">
        <TextBlock><Run Text="Name:" /> <Run Text="{Binding Name, FallbackValue=Name, Mode=OneWay}" /></TextBlock>
    </Grid>
</UserControl>
错误:
A TwoWay or OneWayToSource binding cannot work on the read-only property 'Name'
我正在尝试将DataBinding从我的ViewModel转换为只读属性。 我已经将绑定模式设置为OneWay ..但它仍然会抛出上述错误。 我没有线索! 任何帮助,将不胜感激。
要使用OneWay绑定,您的属性应该已获取并设置。 所以在这种情况下,为了解决这个问题,你只需要像下面这样设置你的属性:
public string Name { private set; get { return _model.Name; } 
上一篇: only property even with OneWay mode set
下一篇: Push DependencyProperty back to ViewModel upon DataContext Change
