I ran in to the unique situation today where I needed to bind the Visible
property of a button in a DataGridRow
to be based on both a property of the bound object and of the model backing it.
XAML:
<t:DataGrid ItemsSource=开发者_开发问答"{Binding Items}">
<t:DataGrid.Columns>
<t:DataGridTemplateColumn>
<t:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Visibility="IsEditable OR IsAdmin"/>
</DataTemplate>
</t:DataGridTemplateColumn.CellTemplate>
</t:DataGridTemplateColumn>
</t:DataGrid.Columns>
</t:DataGrid>
Model:
class TheModel
{
public ObservableCollection<Whatever> Items { get; set; }
public bool IsAdmin { get; set; }
}
Class:
class Whatever
{
public bool IsEditable { get; set; }
}
This stumped me. The only concept that I could think might work would be somehow passing the bound object and either the entire model or just the IsAdmin
property to a static method on a converter or something. Any ideas?
Firstly, you cannot directly use a Boolean for Visibility. You need to use the BooleanToVisibilityConverter.
Secondly, about the OR, you have different options:
Create a readonly property
IsEditableOrAdmin
inWhatever
which returns the value you want. Drawback: YourWhatever
will need a back-reference toTheModel
.Use a MultiBinding and write an IMultiValueConverter. Then, pass both values in the MultiBinding. Since
TheModel
is no longer in the DataContext scope at that point, you could use theElementName
property of the Binding to refer to a UI element whereTheModel
is still accessible.Example (untested):
<SomeElementOutsideYourDataGrid Tag="{Binding TheModel}" /> ... <Button> <Button.Visibility> <MultiBinding Converter="{StaticResource yourMultiValueConverter}"> <Binding Path="IsEditable" /> <Binding ElementName="SomeElementOutsideYourDataGrid" Path="Tag.IsAdmin"/> </MultiBinding> </Button.Visibility> </Button>
Use a more powerful binding framework such as PyBinding.
精彩评论